2012-06-12 47 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

相关问题