赞
踩
1. 方法:使用 lambda
(1) lambda: func1(1, 2) # 单个函数
(2) lambda: [func1(1, 2), func2(a, b)] # 多个函数
2. 说明
(1) 函数必须带 '()', 无论是否有参数
(2) 调用多个函数时,'[]' 不能省略
import tkinter def func(): print("Click !") def main(): window = tkinter.Tk() window.title('无参数') window.geometry('300x100+500+300') btn = tkinter.Button(text='按钮', width=10, height=3, command=func) btn.pack(pady=10) window.mainloop() if __name__ == '__main__': main()
import tkinter def func(x, y): print(f'x = {x}, y = {y}') def main(): window = tkinter.Tk() window.title('有参数') window.geometry('300x100+500+300') btn = tkinter.Button(text='按钮', width=10, height=3, command=lambda: func(1, 2)) btn.pack(pady=10) window.mainloop() if __name__ == '__main__': main()
import tkinter def func1(a): print(f'Say: a = {a}') def func2(a, b): print(f'Say: a = {a}, b = {b}') def main(): window = tkinter.Tk() window.title('无参数') window.geometry('300x100+500+300') btn = tkinter.Button(text='按钮', width=10, height=3, command=lambda: [func1(1), func2(1, 2)]) btn.pack(pady=10) window.mainloop() if __name__ == '__main__': main()
from tkinter import * import logging class Window: def __init__(self): self.window = Tk() self.window.title('演示写入数据') # 获取屏幕大小 screenwidth = self.window.winfo_screenwidth() # 屏幕宽 screenheight = self.window.winfo_screenheight() # 屏幕高 # 窗口居中 size = self.window_of_center(screenwidth, screenheight) self.window.geometry(size) self.window.resizable(0, 0) # 禁止调整大小 # 输入的数据 self.message = StringVar() # 页面布局 self.layout() self.window.mainloop() @staticmethod def window_of_center(screenwidth, screenheight, width=400, height=200): """ 设置窗口居中 :param screenwidth: 屏幕宽 :param screenheight: 屏幕高 :param width: 窗体宽 :param height: 窗体高 :return: 窗体大小及位置 size """ # 设置窗口在屏幕中间 size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) logging.info(f'屏幕(宽 x 高): {screenwidth} x {screenheight}') logging.info(f'窗口(宽 x 高): {width} x {height}') return size def layout(self): """ 页面布局 :return: """ # 标签 hint = Label(self.window, text='请输入:', fg='blue', font=('宋体', 10), anchor='e') entry = Entry(self.window, bd=1, textvariable=self.message) btn = Button(self.window, text='确定', command=lambda: self.show_text(text)) text = Text(self.window, width=45, height=10) # 布局 hint.place(x=5, y=10, width=80, height=20) entry.place(x=95, y=10, width=200, height=20) btn.place(x=300, y=10, height=20) text.place(x=40, y=50) def show_text(self, text): """ 插入文本内容,带参数 :param text: 文本框 :return: """ # 写入 text text.insert(INSERT, self.message.get()) logging.info(f'写入 message = {self.message.get()}') # 清空 self.message.set('') logging.info('清空 message 成功') if __name__ == '__main__': # 设置日志等级 logger = logging.getLogger() logger.setLevel(logging.INFO) # 调用窗体 Window()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。