芯查查logo
  • 数据服务
    1. 新产品
    2. 物料选型
    3. 查替代
    4. 丝印反查
    5. 查品牌
    6. PCN/PDN
    7. 查方案
    8. 查代理
    9. 数据合作
  • SaaS/方案
      SaaS产品
    1. 供应链波动监控
    2. 半导体产业链地图
    3. BOM管理
    4. 解决方案
    5. 汽车电子
    6. 政府机构
    7. 数据API
  • 商城
  • 行业资讯
    1. 资讯
    2. 直播
  • 论坛社区
    1. 论坛
    2. 学习
    3. 测评中心
    4. 活动中心
    5. 积分商城
  • 查一下
  • 开通会员
俊逸的西牛
昉星光2测试python程序运行

在安装完python系统之后,按照官方给出的方法装载python(https://zhuanlan.zhihu.com/p/637592241?utm_id=0)

之后编写了一个小的计算器程序,用以测试在VF2上python程序的运行效果,程序如下:

    from tkinter import *
   import tkinter.font as tkFont
   from functools import partial
   def get_input(entry, argu):
        entry.insert(END, argu)
   def back_space(entry):
        input_len=len(entry.get())
        entry.delete(input_len-1)
   def delete(entry):
       entry.delete(0, END)
   def calc(entry):
       input=entry.get()
       output=str(eval(input.strip()))
       delete(entry)
       entry.insert(END,output)
   def main():
       root=Tk()
       root.title("计算器-VisionFive2")
       root.resizable(0,0)
       entry_font=tkFont.Font(size=16)  
       entry=Entry(root, justify="right", font=entry_font)
       entry.grid(row=0, column=0, columnspan=5, sticky=W+E+N+S, padx=5, pady=5)
       button_font=tkFont.Font(size=16, weight=tkFont.BOLD)
       button_bg='#AABBBB'
       button_active_bg='#55EE55'
       myButton=partial(Button, root, bg=button_bg, padx=15, pady=12, activebackground=button_active_bg)
       button7=myButton(text='7',command=lambda : get_input(entry, '7'))
       button7.grid(row=1, column=0, pady=5)
       button8=myButton(text='8',command=lambda : get_input(entry, '8'))
       button8.grid(row=1, column=1, pady=5)
       button9=myButton(text='9',command=lambda : get_input(entry, '9'))
       button9.grid(row=1, column=2, pady=5)
       button10=myButton(text='+',command=lambda : get_input(entry, '+'))
       button10.grid(row=1, column=3, pady=5)
       button4=myButton(text='4',command=lambda : get_input(entry, '4'))
       button4.grid(row=2, column=0, pady=5)
       button5=myButton(text='5',command=lambda : get_input(entry, '5'))
       button5.grid(row=2, column=1, pady=5)
       button6=myButton(text='6',command=lambda : get_input(entry, '6'))
       button6.grid(row=2, column=2, pady=5)
       button11=myButton(text='-',command=lambda : get_input(entry, '-'))
       button11.grid(row=2, column=3, pady=5)
       button1=myButton(text='1',command=lambda : get_input(entry, '1'))
       button1.grid(row=3, column=0, pady=5)
       button2=myButton(text='2',command=lambda : get_input(entry, '2'))
       button2.grid(row=3, column=1, pady=5)
       button3=myButton(text='3',command=lambda : get_input(entry, '3'))
       button3.grid(row=3, column=2, pady=5)
       button12=myButton(text='*',command=lambda : get_input(entry, '*'))
       button12.grid(row=3, column=3, pady=5)
       button0=myButton(text='0',command=lambda : get_input(entry, '0'))
       button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=W+E+N+S)
       button13=myButton(text='.',command=lambda : get_input(entry, '.'))
       button13.grid(row=4, column=2, pady=5)
       button14=myButton(text='/',command=lambda : get_input(entry, '/'))
       button14.grid(row=4, column=3, pady=5)
       button15=myButton(text='<-',command=lambda : back_space(entry))
       button15.grid(row=5, column=0, pady=5)
       button16=myButton(text='C',command=lambda : delete(entry))
       button16.grid(row=5, column=1, pady=5)
       button17=myButton(text='=',command=lambda : calc(entry))
       button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=W+E+N+S)
       root.mainloop()
   if __name__ == '__main__':
       main()

解读一下其中的一些功能:
1.程序从tkinter模块中导入了所有内容,然后导入了tkinter.font模块以设置字体样式,最后导入了functools模块的partial函数,用于部分应用函数。
2.get_input函数接收一个entry(输入框)和一个argu(要插入的字符串),然后在entry的末尾插入argu。
3.back_space函数获取entry中的文本长度,然后删除最后一个字符。
4.delete函数获取entry中的文本,然后清空entry。
5.calc函数获取entry中的文本,然后清空entry,并在末尾插入一个空字符串。
6.main函数是程序的主入口点。它首先创建一个Tk窗口,并设置了窗口的标题和大小。然后它创建了一个Entry控件,用于用户输入数字和运算符。接着它定义了一些按钮的样式和行为,包括数字、运算符、退格键、清除键和等号键。最后,它启动了Tk的事件循环,等待用户的交互操作。
7.如果这个脚本被当作主程序运行(而不是被导入到其他脚本中),那么它会调用main函数来运行计算器程序。
相关运行效果可以见附件中的视频。

 

工程附件
python程序测试.rar
一起学习
版块: 赛昉科技
2023/12/21 21:20
  • 举报
😁😂😃😄😅😆😉😊😋😌😍😏😒😓😔😖😘😚😜😝😞😠😡😢😣😤😥😨😩😪😫😭😰😱😲😳😵😷😸😹😺😻😼😽😾😿🙀🙅🙆🙇🙈🙉🙊🙋🙌🙍🙎🙏✂✅✈✉✊✋✌✏✒✔✖✨✳✴❄❇❌❎❓❔❕❗❤➕➖➗➡➰🚀🚃🚄🚅🚇🚉🚌🚏🚑🚒🚓🚕🚗🚙🚚🚢🚤🚥🚧🚨🚩🚪🚫🚬🚭🚲🚶🚹🚺🚻🚼🚽🚾🛀Ⓜ🅰🅱🅾🅿🆎🆑🆒🆓🆔🆕
@好友

全部评论

加载中
游客登录通知
已选择 0 人
自定义圈子
移动
发布帖子
发布动态
发布问答
最新帖子
【工程师经验】+ 飞线【开源】zvs142857 2.1A,3A快充 充电宝迷你UC3842 开关电源优化全解析:攻克常见难题,提升性能飞腾CPU × DeepSeek大模型,双芯加持全系列稳跑速进!解锁芯查查核心用户 2024 活动宝藏经历
热门版块
查看更多
维修技术
电子元器件
电子DIY
每日打卡
汽车电子工程师论坛
工业电子专区
新手入门指南
单片机/MCU论坛
PCB设计
开源项目

155

收藏

分享

微信扫码
分享给好友

评论