|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 qin_yin 于 2020-11-26 22:28 编辑
import tkinter as tk
import tkinter as tk
root = tk.Tk()
root.title('This is My Tk')
root.geometry('300x300')
label = tk.Label(master=root, text='What language do you like ', bg='yellow', width=22, height=4, font=('微软雅黑', 15))
label.pack()
def print_choice():
if var1 == 1 and var2 == 1:
label.config(text='I like it all')
elif var1 == 1 and var2 == 0:
label.config(text='I like it python')
elif var1 == 0 and var2 == 1:
label.config(text='I like C++')
else:
label.config(text='I don`t like it')
var1 = tk.StringVar()
var2 = tk.StringVar()
py_button = tk.Checkbutton(master=root, text='Pyhton',width=20, height=4, font=('微软雅黑', 15,),
variable=var1, onvalue=1, offvalue=0, command=print_choice)
C_button = tk.Checkbutton(master=root, text='C++', width=20,height=4, font=('微软雅黑',15),
variable=var2, onvalue=1, offvalue=0, command=print_choice)
py_button.pack()
C_button.pack()
root.mainloop()
问题:无论我怎么点击label内容只会发生一次改变,(改变成 I don`t like it),为什么默认选项是勾上的,我想让Checkbutton默认没有被勾上
本帖最后由 Twilight6 于 2020-11-26 22:38 编辑
想实时刷新 text 需要用 textvariable 参数 而不是 text
而且组件中 var1、var2 需要 .get() 方法才能获取属性
而且获取返回的是字符串,所以函数 if 判断那都需要更改
import tkinter as tk
root = tk.Tk()
root.title('This is My Tk')
root.geometry('300x300')
v = tk.StringVar()
v.set('What language do you like ')
label = tk.Label(master=root, textvariable=v, bg='yellow', width=22, height=4, font=('微软雅黑', 15))
label.pack()
def print_choice():
if var1.get() == '1' and var2.get() == '1':
v.set('I like it all')
elif var1.get() == '1' and var2.get() == '0':
v.set('I like it python')
elif var1.get() == '0' and var2.get() == '1':
v.set('I like C++')
else:
v.set('I don`t like it')
var1 = tk.StringVar()
var1.set(0)
var2 = tk.StringVar()
var2.set(0)
py_button = tk.Checkbutton(master=root, text='Pyhton',width=20, height=4, font=('微软雅黑', 15,),
variable=var1, onvalue=1, offvalue=0, command=print_choice)
C_button = tk.Checkbutton(master=root, text='C++', width=20,height=4, font=('微软雅黑',15),
variable=var2, onvalue=1, offvalue=0, command=print_choice)
py_button.pack()
C_button.pack()
root.mainloop()
|
|