2015-02-10 103 views
2

我使用Python的tkinter庫來創建一個小的GUI。該應用程序的代碼如下:圖像建立後爲什麼窗口小部件稍後會先出現?

import tkinter as tk 
from tkinter import ttk 
APP_TITLE = "HiDE" 

class Application(tk.Frame): 
    """This class establishes an entity for the application""" 
    #The constructor of this class 
    def __init__(self, master=None):   
     tk.Frame.__init__(self,master) 
     self.grid() 

    def setupWidgets(self): 
     self.message = tk.Label(self,text='Login') 
     self.quitButton = tk.Button(self,text='Quit',command=self.quit) 
     self.logButton = tk.Button(self,text='Login',command=self.quit) 
     self.master.title(APP_TITLE) 
     self.master.minsize("300","300") 
     self.master.maxsize("300","300") 
     self.message.grid() 
     self.logButton.grid() 
     self.quitButton.grid() 

#Setting up the application 
app = Application() 
img = tk.PhotoImage(file='icon.png') 
#getting screen parameters 
w = h = 300#max size of the window 
ws = app.master.winfo_screenwidth() #This value is the width of the screen 
hs = app.master.winfo_screenheight() #This is the height of the screen 

# calculate position x, y 
x = (ws/2) - (w/2) 
y = (hs/2) - (h/2) 
#This is responsible for setting the dimensions of the screen and where it is placed 
app.master.geometry('%dx%d+%d+%d' % (w, h, x, y)) 
app.master.tk.call('wm', 'iconphoto', app.master._w, img) 
app.master.i_con = tk.Label(app.master, image=img) 
app.master.i_con.grid() 
app.setupWidgets() #<-- This is the function call 
app.mainloop() 

setupWidgets函數被調用,但輸出是:

The output

回答

4

到時候你格標籤與圖像,你已經在你的課堂上的框架上調用了網格。這個類是其他小部件的放置位置,因此它被放置在帶有圖像的標籤上方。

而不是

app.master.i_con = tk.Label(app.master, image=img) 

嘗試

app.master.i_con = tk.Label(app, image=img) 

把標籤與圖像與其他部件的框架。


在附註中,沒有指定行和列的調用網格並不合理。

+0

在我看到這個之前,我還沒有真正開始定位小部件。這就是爲什麼沒有行和列。 – bluefog 2015-02-10 18:56:21

相關問題