python的资料还是少啊
如下所示,按着书上的例子来的,stringvar的运行后初试显示是两个都是选中了,Radiobutton不是一组都能选中么。。而用intvar就正常了,查了半天也不知道怎么回事。。。TKinter的资料真少啊。。
from tkinter import * #导入全部的tkinter库
class changelabeldemo:
def __init__ (self):
window = Tk()
window.title("change label demo")
#创建一个label
frame1 = Frame(window)
frame1.pack()
self.label1 = Label(frame1, text = "programming is fun") #不加self.的话作用域只在这个中
self.label1.pack() #加了self.作用域扩大为整个class类
frame2 = Frame(window)
frame2.pack()
label2 = Label(frame2, text = "Enter text")
self.msg = StringVar()
entry = Entry(frame2, textvariable = self.msg)
btChangeText = Button(frame2, text = "change text", command = self.processButton)
self.v1 = IntVar()
rdRed = Radiobutton(frame2, text = "red", bg = "red", variable = self.v1,
value = 1, command = self.processRadiobutton)
rdYellow = Radiobutton(frame2, text = "yellow", bg = "yellow", variable = self.v1,
value = 2, command = self.processRadiobutton)
label2.grid(row = 1, column = 1)
entry.grid(row = 1, column = 2)
btChangeText.grid(row = 1, column = 3)
rdRed.grid(row = 1, column = 4)
rdYellow.grid (row = 1, column = 5)
window.mainloop()
def processButton(self):
self.label1["text"] = self.msg.get()
def processRadiobutton(self):
if self.v1.get() == 1:
self.label1["fg"] = "red"
elif self.v1.get() == 2:
self.label1["fg"] = "yellow"
changelabeldemo()
如下所示,按着书上的例子来的,stringvar的运行后初试显示是两个都是选中了,Radiobutton不是一组都能选中么。。而用intvar就正常了,查了半天也不知道怎么回事。。。TKinter的资料真少啊。。
from tkinter import * #导入全部的tkinter库
class changelabeldemo:
def __init__ (self):
window = Tk()
window.title("change label demo")
#创建一个label
frame1 = Frame(window)
frame1.pack()
self.label1 = Label(frame1, text = "programming is fun") #不加self.的话作用域只在这个中
self.label1.pack() #加了self.作用域扩大为整个class类
frame2 = Frame(window)
frame2.pack()
label2 = Label(frame2, text = "Enter text")
self.msg = StringVar()
entry = Entry(frame2, textvariable = self.msg)
btChangeText = Button(frame2, text = "change text", command = self.processButton)
self.v1 = IntVar()
rdRed = Radiobutton(frame2, text = "red", bg = "red", variable = self.v1,
value = 1, command = self.processRadiobutton)
rdYellow = Radiobutton(frame2, text = "yellow", bg = "yellow", variable = self.v1,
value = 2, command = self.processRadiobutton)
label2.grid(row = 1, column = 1)
entry.grid(row = 1, column = 2)
btChangeText.grid(row = 1, column = 3)
rdRed.grid(row = 1, column = 4)
rdYellow.grid (row = 1, column = 5)
window.mainloop()
def processButton(self):
self.label1["text"] = self.msg.get()
def processRadiobutton(self):
if self.v1.get() == 1:
self.label1["fg"] = "red"
elif self.v1.get() == 2:
self.label1["fg"] = "yellow"
changelabeldemo()