2012-06-12 52 views
2

我是新來的編程和python,所以我提前感謝您的耐心。python - TKinter - 我可以發送背景嗎?

我創建了一個類,它爲背景創建一個帶有測試圖像的新窗口(「test1.gif」)。

我還在同一個班級中創建了一個下拉菜單,讓您在3個國家之間進行選擇。

我可以評論使背景或使菜單和其他將顯示的代碼的代碼。不過,我希望菜單出現在背景之上。當我運行完整的代碼時,我只能看到背景。

我很欣賞我可能在這裏丟失了一些非常明顯的東西,如果有人能指出這是什麼。

謝謝你的時間。 丹

from Tkinter import * 
import Tkinter as tk 



class dropDown(): 

    def __init__(self, master): 


     #background set_up 
     image1 = tk.PhotoImage(file="test1.gif") 
     w = image1.width() 
     h = image1.height() 
     root.geometry("%dx%d+0+0" % (w, h)) 
     panel1 = tk.Label(root, image=image1) 
     panel1.pack(side='top', fill='both', expand='yes') 

     # save the panel's image from 'garbage collection' 
     panel1.image = image1 


     #Drop down menu 
     self.var = StringVar(master) 
     self.var.set('Alaska') # initial value 

     #Have not used countries list just pasted 
     self.option = OptionMenu(master, self.var, 'Alberta', 'Australia') 
     self.option.pack() 



root = Tk() 
root.title('drop down test') 
dropDown(root) 
mainloop() 

回答

2

(我覺得)你的問題是,你限制了根窗口的大小爲圖像(代替的sizeof(圖像)+的sizeof(菜單))的大小。這解決了OS-X上的問題。

import Tkinter as tk 

class dropDown(): 
    def __init__(self, master): 

     #background set_up 
     image1 = tk.PhotoImage(file="test.gif") 
     w = image1.width() 
     h = image1.height() 
     panel1 = tk.Label(root, image=image1) 
     panel1.pack(side='top', fill='both', expand='yes') 

     # save the panel's image from 'garbage collection' 
     panel1.image = image1 

     #Drop down menu 
     self.var = tk.StringVar(master) 
     self.var.set('Alaska') # initial value 

     #Have not used countries list just pasted 
     self.option = tk.OptionMenu(master, self.var, 'Alaska','Alberta', 'Australia') 
     self.option.pack() 

     geomstr="%dx%d+0+0" % (w, panel1.winfo_reqheight()+self.option.winfo_reqheight()) 
     root.geometry(geomstr) 

root = tk.Tk() 
root.title('drop down test') 
dropDown(root) 
root.mainloop() 

一個側面說明,這將是很容易讓你的下拉框,從繼承,然後調整幀的大小(雖然它可能不是必要的)。然後,您可以在GUI上的任何位置移動此小部件。

編輯

子類框架:

import Tkinter as tk 

class dropDown(tk.Frame): 
    def __init__(self, master): 
     tk.Frame.__init__(self,master) 

     #background set_up 
     image1 = tk.PhotoImage(file="test.gif") 
     panel1 = tk.Label(root, image=image1) 
     panel1.pack(side='top', fill='both', expand='yes') 

     # save the panel's image from 'garbage collection' 
     panel1.image = image1 

     #Drop down menu 
     self.var = tk.StringVar(master) 
     self.var.set('Alaska') # initial value 

     #Have not used countries list just pasted 
     self.option = tk.OptionMenu(master, self.var, 'Alaska','Alberta', 'Australia') 
     self.option.pack() 


root = tk.Tk() 
root.title('drop down test') 
d=dropDown(root) 
d.pack(side='top',fill='both',expand='yes') 
root.mainloop() 

需要注意的是,現在的Tkinter/TK把所有的幀大小調整爲你的照顧。 :)

+0

感謝mgilson,它會把我永遠拿出來工作!我想我理解你的解決方案。我會看看我是否可以解決如何讓框架繼承你所建議的下拉菜單。再次感謝。 – hemmy

+0

@ user1451238 - 我也在爲此撓了撓頭。 – mgilson

+0

@ user1451238 - 我發佈了一個框架子類化的例子。隨意修改,只要你認爲合適。 – mgilson

相關問題