gpt4 book ai didi

python - 向 Tkinter 添加单选按钮

转载 作者:行者123 更新时间:2023-11-30 23:49:05 25 4
gpt4 key购买 nike

我和我的 friend 正在制作一个显示 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

最佳答案

您需要实际初始化 App 类才能执行任何操作。标签应该与所有其他小部件一起进入 init 方法内部。通常,您会将所有小部件放在框架内(例如,Radiobutton(frame, text= ...),但如果您不打算使用框架,则无需创建它。并对齐单选按钮(与 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()

关于python - 向 Tkinter 添加单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7775001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com