tkinter.Button触发的函数有返回值的示例

时间: 2024-04-29 22:21:31 浏览: 8
在Tkinter中,Button的触发函数是没有返回值的。但是,我们可以在触发函数中调用其他函数,让其他函数返回值给我们。 以下是一个示例代码,演示了如何通过调用其他函数来获取返回值: ```python import tkinter as tk def calculate(): num1 = int(entry1.get()) num2 = int(entry2.get()) result = num1 + num2 return result def print_result(): result = calculate() label3.config(text=result) root = tk.Tk() label1 = tk.Label(root, text="Number 1:") label1.pack() entry1 = tk.Entry(root) entry1.pack() label2 = tk.Label(root, text="Number 2:") label2.pack() entry2 = tk.Entry(root) entry2.pack() button = tk.Button(root, text="Calculate", command=print_result) button.pack() label3 = tk.Label(root, text="") label3.pack() root.mainloop() ``` 在这个例子中,我们定义了一个`calculate()`函数,用于计算两个数的和。然后我们定义了一个`print_result()`函数,它调用`calculate()`函数来获取结果,并将结果显示在标签上。 注意,我们将`print_result()`函数作为Button的触发函数,而不是`calculate()`函数。这是因为Button的触发函数必须是没有返回值的。但是,我们可以在`print_result()`函数中调用`calculate()`函数,并获取它的返回值。

相关推荐

import tkinter import tkinter.messagebox def Button_Click(btn): btn_Label = tkinter.Entry(mainUI) btn_Label.insert(tkinter.END,btn) btn_Label.place(x=45,y=75,width=40,height=20) mainUI = tkinter.Tk() mainUI.title('计算器') mainUI.geometry('300x280') #输入数字的两个文本框 num1_Label = tkinter.Label(mainUI,text='number 1 :',justify=tkinter.LEFT,anchor='e') num1_Label.place(x=25,y=20,width=80,height=30) num2_Label = tkinter.Label(mainUI,text='number 2 :',justify=tkinter.LEFT,anchor='e') num2_Label.place(x=25,y=125,width=80,height=30) num1_Var = tkinter.StringVar(mainUI,value='') num2_Var = tkinter.StringVar(mainUI,value='') num1_Entry = tkinter.Entry(mainUI,width=100,textvariable=num1_Var) num1_Entry.place(x=110,y=25,width=140,height=20) num2_Entry = tkinter.Entry(mainUI,width=100,textvariable=num2_Var) num2_Entry.place(x=110,y=130,width=140,height=20) #创建加减乘除按钮及显示文本框 sum_Button = tkinter.Button(mainUI,text='+',command=lambda :Button_Click('+')) sum_Button.place(x=100,y=75,width=30,height=20) min_Button = tkinter.Button(mainUI,text='-',command=lambda :Button_Click('-')) min_Button.place(x=145,y=75,width=30,height=20) mul_Button = tkinter.Button(mainUI,text='x',command=lambda :Button_Click('x')) mul_Button.place(x=190,y=75,width=30,height=20) div_Button = tkinter.Button(mainUI,text='/',command=lambda :Button_Click('/')) div_Button.place(x=235,y=75,width=30,height=20) #创建结果框以及计算按钮 cal_Label = tkinter.Label(mainUI,text='result :',justify=tkinter.LEFT,anchor='e') cal_Label.place(x=25,y=175,width=80,height=20) cal_Entry = tkinter.Entry(mainUI) cal_Entry.place(x=110,y=175,width=80,height=20) cal_Button = tkinter.Button(mainUI,text='Calculate') cal_Button.place(x=110,y=225,width=80,height=20) mainUI.mainloop()完善此代码中Calculate按钮被按下后进行的计算及输出操作

修改代码使其能实现动态表情包的发送和显示#表情包模块 #用四个按钮定义四种表情包 b1 = b2 = b3 =b4 =b5='' #四幅图片 p1 = tkinter.PhotoImage(file='emoji/facepalm.png') p2 = tkinter.PhotoImage(file='emoji/smirk.png') p3 = tkinter.PhotoImage(file='emoji/concerned.png') p4 = tkinter.PhotoImage(file='emoji/smart.png') p5 = tkinter.PhotoImage(file='emoji/tushe.png') #用字典将标识符与表情图片一一对应 dic = {'aa**':p1,'bb**':p2,'cc**':p3,'dd**':p4,'ff**':p5} ee = 0 #表情面板开关标志 #发送表情的函数 def send_mark(exp): ''' :param exp: 表情图片对应的标识符 :return: ''' global ee mes = exp +':;'+user+':;'+chat_to s.send(mes.encode()) b1.destroy() b2.destroy() b3.destroy() b4.destroy() b5.destroy() ee = 0 #四种表情包的标识符发送函数 def bb1(): send_mark('aa**') def bb2(): send_mark('bb**') def bb3(): send_mark('cc**') def bb4(): send_mark('dd**') def bb5(): send_mark('ff**') #表情包面包操控函数 def express_board(): global b1,b2,b3,b4,b5,ee if ee == 0: #打开表情包面板 ee = 1 b1 = tkinter.Button(root,command=bb1,image=p1,relief=tkinter.FLAT,bd=0) b2 = tkinter.Button(root,command=bb2,image=p2,relief=tkinter.FLAT,bd=0) b3 = tkinter.Button(root,command=bb3,image=p3,relief=tkinter.FLAT,bd=0) b4 = tkinter.Button(root,command=bb4,image=p4,relief=tkinter.FLAT,bd=0) b5 = tkinter.Button(root,command=bb5,image=p5,relief=tkinter.FLAT,bd=0) b1.place(x=5,y=248) b2.place(x=75,y=248) b3.place(x=145, y=248) b4.place(x=215, y=248) b5.place(x=285, y=248) else: #关闭表情包面板 ee = 0 b1.destroy() b2.destroy() b3.destroy() b4.destroy() b5.destroy() #表情包面板开关按钮 eBut = tkinter.Button(root,text='表情包',command=express_board) eBut.place(x=5,y=320,width=60,height=30)

最新推荐

recommend-type

基于SpringBoot框架仿stackOverflow网站后台开发.zip

基于springboot的java毕业&课程设计
recommend-type

基于SpringBoot洗衣店管理系统.zip

基于springboot的java毕业&课程设计
recommend-type

【优化覆盖】算术算法求解传感器覆盖优化问题【含Matlab源码 2436期】.zip

【优化覆盖】算术算法求解传感器覆盖优化问题【含Matlab源码 2436期】.zip
recommend-type

【优化覆盖】蜣螂算法DBO求解无线传感器WSN覆盖优化问题【含Matlab源码 3567期】.zip

【优化覆盖】蜣螂算法DBO求解无线传感器WSN覆盖优化问题【含Matlab源码 3567期】.zip
recommend-type

FusionCompute修改VRM节点IP地址

FusionCompute修改VRM节点IP地址 该任务指导工程师对VRM节点的IP地址、主机的管理IP地址进行修改。 执行该任务时应注意: • 建议同时修改VRM和主机的管理IP。如果修改了VRM的IP,会导致本地PC与VRM的连接短暂中断。 • 修改前应已完成网络规划,并在FusionCompute中确认VRM节点运行正常,所有主机运行正常(无处于异常或维护状态的主机)。 • 如果跨网段修改IP地址时,则应注意在完成所有节点IP地址的修改后,在相应的汇聚交换机进行配置,保证修改后的主机IP地址、VRM节点及本地PC之间能进行正常通信。相关交换机配置命令,请参考交换机配置样例。 • 如果跨网段修改管理IP地址,同时涉及修改管理VLAN,请先修改管理平面VLAN,待修改完成,且各节点与VRM网络通信正常后,再进行修改VRM IP地址和主机IP地址的操作。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。