2015-06-22 79 views
1

我正在通過在線教程學習如何使用Tkinter製作GUI。但是我在使用.grid()函數時遇到問題。當使用.grid時,Python Tkinter未打開()

這裏是我到目前爲止有:

from Tkinter import * 

root = Tk() 
title = Label(root, text = "Hello World") 
title.pack() 

name   = Label(root, text = "Name") 
password  = Label(root, text = "Password") 
entry_name  = Entry(root) 
entry_password = Entry(root) 

name.grid   (row = 0, sticky = E) 
password.grid  (row = 0, sticky = E) 
entry_name.grid  (row = 0, column = 1) 
entry_password.grid (row = 1, column = 1) 

check = Checkbutton(root, text = "Keep me logged in") 
check.grid(columnspan = 2) 

root.mainloop() 

因此,有問題的IM是,只要我包括第一行:

name.grid(row = 0, sticky = E) 

然後運行該腳本,有沒有錯誤,但沒有打開。該程序只是掛起,我必須關閉命令提示符才能重新獲得控制權。

如果我使用.grid()註釋掉所有行,它們的程序將正常工作,並會打開一個窗口,其中包含我的標題。

那麼,有沒有人知道我做錯了什麼? 即時通訊使用Python 2.7

+0

看起來你已經在'root'內使用'pack()'了。混合幾何管理器通常會產生某種錯誤信息,但是您是否嘗試過修復? – TigerhawkT3

+0

您不能使用包和網格 –

回答

4

您不能同時使用兩個或多個共享同一父級的小部件,這兩個小部件都可以使用packgrid。在你的情況下,你使用packtitlegrid的一切。使用一個或另一個。

3

保持網格幾何管理器一致,所以使用title.grid()。

from tkinter import * 

root = Tk() 
title = Label(root, text = "Hello World") 
title.grid() 

name   = Label(root, text = "Name") 
password  = Label(root, text = "Password") 
entry_name  = Entry(root) 
entry_password = Entry(root) 

name.grid(row = 0, sticky = E) 
password.grid  (row = 0, sticky = E) 
entry_name.grid  (row = 0, column = 1) 
entry_password.grid (row = 1, column = 1) 

check = Checkbutton(root, text = "Keep me logged in") 
check.grid(columnspan = 2) 

root.mainloop() 
+0

謝謝。初學者錯誤。 –

+0

不用擔心 - 去過那裏! – sw123456