本帖最后由 阎兆辉 于 2018-10-23 16:52 编辑
创建控制变量
var = StringVar()
设置控制变量的值
var.set("set a value.")
获得控制变量的值
var.get("get a value from the control parameter.")
控制变量使用实例
import tkinter as tk
root = tk.Tk()
# 创建一个label标签,在上面存放位图或者文字
labelx = tk.Label(root, bitmap='questhead', compound=tk.LEFT)
var = tk.StringVar()
var.set("This is a dialogue.")
labelx.config(textvariable=var)
labelx.config(bg='cyan')
labelx.config(font=('Times New Roman', 20, 'bold'))
labelx.config(relief=tk.FLAT)
labelx.config(bd=5)
# 点击问题按钮后,反应的函数
def askvar():
var.set("How are you?")
# 点击回答按钮后,反应的函数
def responsevar():
var.set("I'm fine. Thank you for your asking.")
# 按钮1创建
button1 = tk.Button(root, text="问题", command=askvar)
button1.config(bg='yellow')
button1.config(font=('楷书', 20, 'italic'))
button1.config(relief=tk.GROOVE)
# 按钮2创建
button2 = tk.Button(root, text="回答", command=responsevar)
button2.config(bg='red')
button2.config(font=('楷书', 20, 'italic'))
button2.config(relief=tk.RIDGE)
# 对创建的组件进行排兵布阵
labelx.pack(side=tk.TOP)
button1.pack(side=tk.LEFT)
button2.pack(side=tk.RIGHT)
root.mainloop()
|
|