我的朋友和我正在制作一个显示4个单选按钮,文本的窗口,我们正在努力在底部添加两个按钮。
这是我们的代码:
from Tkinter import *
import time
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
v = IntVar()
Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP)
Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP)
Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP)
Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP)
self.button = Button(master, text="BACK", fg="red", command=self.button6)
self.button.pack(side=BOTTOM)
self.button = Button(master, text="NEXT", fg="red", command=self.button5)
self.button.pack(side=BOTTOM)
def button6(self):
print "Sam is awesome!GAJONGA"
def button5(self):
print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY"
w = Label(master, text="1. Anxiety, nervousness, worry or fear")
w.pack()
master = Tk()
master.mainloop()
运行此操作会给我们带来以下错误:
Traceback (most recent call last):
File "/Volumes/CHROME USB/STORAGE/TKinker GUI/Radiobutton example.py", line 36,
in <module>
w = Label(master, text="1. Anxiety, nervousness, worry or fear")
NameError: name 'master' is not defined
答案 0 :(得分:3)
您需要实际初始化App类,以便它可以执行任何操作。标签应该与init方法一起进入所有其他小部件。通常你会把所有的小部件都放在框架内(例如,Radiobutton(frame,text = ...),但是如果你不打算使用框架就没有必要创建它。并且对齐radiobuttons(与anchor =“w”)而不是居中使它们看起来更干净。
from Tkinter import *
import time
class App:
def __init__(self, master):
w = Label(master, text="1. Anxiety, nervousness, worry or fear")
w.pack()
v = IntVar()
Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP, anchor="w")
Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP, anchor="w")
Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP, anchor="w")
Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP, anchor="w")
self.button = Button(master, text="BACK", fg="red", command=self.button6)
self.button.pack(side=BOTTOM)
self.button = Button(master, text="NEXT", fg="red", command=self.button5)
self.button.pack(side=BOTTOM)
def button6(self):
print "Sam is awesome!GAJONGA"
def button5(self):
print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY"
master = Tk()
app = App(master)
master.mainloop()
答案 1 :(得分:0)
master
是传递给App.__init__()
的参数;它不存在于此之外,例如在您尝试使用它的全局范围内。