python - 为什么后来声明的小部件首先出现?

标签 python tkinter

我正在使用 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

最佳答案

当您将标签与图像网格化时,您已经在类中的框架上调用了 grid 。此类是放置其他小部件的位置,因此它放置在带有图像的标签上方。

而不是

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

尝试

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

将带有图像的标签与其他小部件一起放入框架中。


顺便说一句,在不指定行和列的情况下调用网格实际上没有意义。

关于python - 为什么后来声明的小部件首先出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28437490/

相关文章:

python - Pandas 在组内移动组子集的值

python - 如何为 suds Web 服务对象设置 "text"值

python - 将框架中的所有条目小部件绑定(bind)到一个键

python - 如何在 Tkinter 中使用列表框打开文本文件

python - 在 Python 中运行 shutil.rmtree(d) 后权限被拒绝执行 os.mkdir(d)

Python re.match 只匹配第一个\n

python - Pycharm调试 watch : Can I show directly an image

python - 在python之前,声音已经完成了吗?

python - Python 中来自 tkinter 的绑定(bind)函数的问题

python 2.7 Tk更改标签以携带打开文件中的文件名