2016-06-14 45 views
0

嗨,我是編程新手,我很抱歉,如果這是一個明顯的錯誤。我正在Mac OSX El Capitan上使用python 3.5編寫一個gui。這是迄今爲止代碼:python3 tkinter gui沒有迴應

from tkinter import * 
from tkinter import ttk 



class GUI(object): 
    def __init__(self, master): 
     master.title("Title") 
     master.resizable(False, False) 
     self.frame1 = ttk.Frame(master) 
     self.frame1.pack() 

     ttk.Label(text="Organism").grid(row=1, column=0) 

     self.organism_picker = ttk.Combobox(self.frame1, values=("Drosophila  melanogaster", 
                  "Danio rerio", 
                  "Caenorhabditis  elegans", 
                  "Rattus  norvegicus", 
                  "Mus musculus", 
                  "Homo sapiens")) 
     self.organism_picker.grid(row=2, column=0) 

     ttk.Label(text="Core gene symbol:").grid(row=3, column=0) 

     self.core = ttk.Entry(self.frame1) 
     self.core.grid(row=4, column=0) 


root = Tk() 
gui = GUI(root) 
root.mainloop() 

當我運行該程序進入主循環,但從來沒有出現在GUI窗口和發射不reponding。 我試圖重新安裝python 3,安裝了ActiveTcl,而不是嘗試使用ActivePython。堅果沒有一個工作。

非常感謝大家的回答。

回答

1

與您的代碼唯一的問題是,你忘了附加兩個標籤到主窗口部件self.frame1

爲了解決這個問題,修改它們如下:

#Attach the 2 labels to self.frame1 
ttk.Label(self.frame1,text="Organism").grid(row=1, column=0) 
ttk.Label(self.frame1,text="Core gene symbol:").grid(row=3, column=0) 

演示

這樣做後,你會得到這樣的:

enter image description here

3

你不能使用包()和電網(),因爲這將產生在此錯誤指出幾何經理衝突:

_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack 

嘗試改變:

self.frame1.pack() 

到:

self.frame1.grid() 

在這種情況下,我會建議使用包總體,因爲這是一個相當簡單的佈局。請參閱this guide

When to use the Pack Manager

Compared to the grid manager, the pack manager is somewhat limited, but it’s much easier to use in a few, but quite common situations:

Put a widget inside a frame (or any other container widget), and have it fill the entire frame Place a number of widgets on top of each other Place a number of widgets side by side

最後:

Note: Don’t mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.