[英]python 3.2 tkinter, text in radio button is not display
所以我一直在使用python 3.2 tkinter。 今天刚发现单选按钮旁边的文字没有显示在按钮旁边,它只显示“0”。 此外,当我在单选按钮语句的末尾有.pack()时,它显示错误'NoneType'对象没有属性'pack'。 它是如此奇怪,是因为他们在新版本中改变了。 我需要输入其他东西吗? 谢谢
from tkinter import*
class Name:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self._var = IntVar()
self._fullRadio = Radiobutton(frame, text="yes", textvariable=self._var, value=1)
self._fullRadio.grid(row=2, column=0)#.pack()
self._partRadio = Radiobutton(frame, text="no", textvariable=self._var, value=2)
self._partRadio.grid(row=3)#.pack()
self._notRadio = Radiobutton(frame, text="not both", textvariable=self._var, value=3)
self._notRadio.grid(row=4)#.pack()
root = Tk()
application = Name(root)
root.mainloop()
你想要参数variable
,而不是textvariable
:
from tkinter import*
class Name:
def __init__(self, master):
frame = Frame(master)
frame.grid() # changed from frame.pack()
self._var = IntVar()
self._fullRadio = Radiobutton(frame, text="yes", variable=self._var, value=1)
self._fullRadio.grid(row=2, column=0)
self._partRadio = Radiobutton(frame, text="no", variable=self._var, value=2)
self._partRadio.grid(row=3)
self._notRadio = Radiobutton(frame, text="not both", variable=self._var, value=3)
self._notRadio.grid(row=4)
root = Tk()
application = Name(root)
root.mainloop()
另外,根据经验,在同一帧中混合.grid()
和.pack()
并不是首选。
至于你的第二个问题: .grid()
是另一个布局管理器。 只做self._fullRadio.grid(row=2, column=0)
已经设置了布局; 除了.grid()
(在同一个对象上)之外,你不需要使用.pack()
)。
你得到一个NoneType
对象没有方法.pack()
的错误,因为self._fullRadio.grid(row=2, column=0)
返回None
(这是一个方法调用)。 坚持grid
或pack
,但不能同时兼顾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.