目标:在Python的UI编程环境中,实现控件之间的关联。本例中窗口中有三个文本框,其中v1和v2是输入,第三个文本框的值等于v1和v2之和。
编程环境:win10、Python3.7.6
说明:
1. 为主类设置三个StringVar类型的属性 v1、v2和v3。v1和v2是输入,v3是输出。v3 = v1 + v2
2. 对v1和v2设置事件跟踪,回调函数为changeValue,代码:
v1.trace('w', self.changeValue)
3. 编写回调函数changeValue
4. 全部源代码如下:
import tkinter as tk
from tkinter import ttk
class MyApp(tk.Tk):
def __init__(self):
super().__init__()
# self.pack()
self.v1 = tk.StringVar(value='10')
self.v2 = tk.StringVar(value='20')
self.v3 = tk.StringVar()
self.changeValue()
self.v1.trace('w', self.changeValue)
self.v2.trace('w', self.changeValue)
self.createUI()
# 响应v1和v2数值变化的回调函数
def changeValue(self, *ignoredArgs):
if self.v1.get(): #当v1值为空时,表示0
v1 = int(self.v1.get())
else:
v1 = 0
if self.v2.get():
v2 = int(self.v2.get())
else:
v2 = 0
self.v3.set(v1+v2)
# 绘制控件,分别绑定三个Entry控件与类属性v1、v2和v3
def createUI(self):
frame1 = ttk.Frame(self)
frame1.pack()
ttk.Label(frame1, text="v1:").grid(column=0, row=0, sticky='W')
v1Entered = ttk.Entry(frame1, width=12, textvariable=self.v1 )
v1Entered.grid(column=1, row=0, sticky='W')
ttk.Label(frame1, text="v2:").grid(column=0, row=1, sticky='W')
v2Entered = ttk.Entry(frame1, width=12, textvariable=self.v2 )
v2Entered.grid(column=1, row=1, sticky='W')
ttk.Label(frame1, text="v1+ v2 = ").grid(column=0, row=2, sticky='W')
v3Entered = ttk.Entry(frame1, width=12, textvariable=self.v3, state='readonly') # 设为只读
v3Entered.grid(column=1, row=2, sticky='W')
if __name__ == '__main__':
app = MyApp()
app.mainloop()