2013-04-11 117 views
0

所以我一直玩python 3.2 tkinter。 今天剛剛發現單選按鈕中的文字沒有顯示在按鈕旁邊,它只顯示「0」。 另外,當我在單選按鈕語句末尾有.pack()時,它顯示錯誤'NoneType'對象沒有屬性'pack'。這很奇怪,是因爲他們在新版本中改變了。我是否需要導入其他一些東西?由於python 3.2 tkinter,單選按鈕中的文本不顯示

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() 

回答

2

你想要的參數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()

由於self._fullRadio.grid(row=2, column=0)返回None(這是一個方法調用),您會收到一個錯誤消息,NoneType對象沒有方法.pack()。堅持要麼gridpack,但不是在同一時間。

+0

哈哈,這就是一個愚蠢的錯誤......感謝您的幫助:) – PyJar 2013-04-11 01:23:28

+1

您可以在同一個應用程序中混合包和網格,只是不在同一個容器(即:不在同一個框架中)。這是因爲每個人都會查看小部件大小的變化,並且(可能)會在更改大小時重新調整其管理的所有窗口的大小。那麼會發生什麼呢,那個網格根據它的規則改變了一些小部件的大小,打包注意到了這些改變,從而根據_its_規則改變了其他小部件的大小。網格注意到所有變化等。然而,在同一個_application_中同時使用grid和pack是Tkinter所有有經驗的編程人員所做的。 – 2013-04-11 10:54:39