简体   繁体   中英

Make a Tkinter element on top of the others in Python

I'm searching for a way to put one element of Tkinter on top of the others in Python.
In the example below, what I tried to do is to put the yellow label on top of the white one - so when we change the size of the window the first label that will disappear is the white one, and only when its no longer visible the yellow one will start to shrink.
How can I realize this idea? Thanks in advance! (;

运行代码示例

The code:

import tkinter as tk

root = tk.Tk()
root.config(background='black')

regularlabel = tk.Label(root, width=20, height=10, text='label')
regularlabel.pack()

bottomlabel = tk.Label(root, text='bottom label', bg='yellow')
bottomlabel.pack(side='bottom', fill='both')

root.mainloop()

Tkinter will shrink widgets in the reverse order of which they were added. Pack the bottom window first to solve your problem.

Since the layout of widgets tends to be changed more than the actual widgets during development, I find the code easier to understand and easier to maintain when layout is separate from widget creation. In the following example I've grouped the two layout statements together.

import tkinter as tk

root = tk.Tk()
root.config(background='black')

regularlabel = tk.Label(root, width=20, height=10, text='label')
bottomlabel = tk.Label(root, text='bottom label', bg='yellow')

bottomlabel.pack(side='bottom', fill='both')
regularlabel.pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM