python - TKinter - 我可以发送背景吗?

标签 python tkinter

我是编程和 Python 新手,所以提前感谢您的耐心等待。

我创建了一个类,它创建一个新窗口,其背景为测试图像(“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()

最佳答案

(我认为)你的问题是你将根窗口的大小限制为图像的大小(而不是 sizeof(image)+sizeof(menu))。这解决了我在 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()

附注,让您的 dropDown 继承自 Frame,然后调整 Frame 的大小将非常容易(尽管可能没有必要)。然后您可以将此小部件移动到 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 会为您处理所有框架大小调整。 :)

关于python - TKinter - 我可以发送背景吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997654/

相关文章:

python-3.x - 有关 Python 3 的 tkinter 和 ttk 的最新教程

python - 如何用python制作一个交互式的简单 watch ?

Python - 检查正在使用哪个子类

python - 使用 Python 从许多 CBR 存档中删除第一个文件

python - 如何摆脱 Pandas/Matplotlib 条形图 x 轴上的虚线?

python - ttk TreeView : alternate row colors

python - 在 CentOS 5 上编译 Python 2.4.3 时,如何明确禁用 _tkinter.c 的编译?

python - 如何使用 Keras 确定类别?

python - 在 python openCV 中一次将许 multimap 像读入内存

Python 多处理,ValueError : I/O operation on closed file