英文:
How to temporarily hide Label text, so it's not all over the tkinter window
问题
这是我的项目的简化版本。我想要一个按钮可以多次使用,但如果我按下它,它将再次显示标签文本。我不知道如何在再次按下按钮后隐藏标签。
```python
from tkinter import *
root = Tk()
root.geometry("300x200")
def show():
global szoveg
if 'szoveg' in globals():
szoveg.pack_forget()
szoveg = Label(root, text="hey")
szoveg.pack()
button = Button(root, text="fasz", command=show)
button.pack()
root.mainloop()
英文:
This is a simplified version of my project. I want a button to use multiple times, but if I press it, it'll display the label text again. I don't know how to hide the label once the button is pressed again.
from tkinter import *
root = Tk()
root.geometry( "300x200" )
def show():
szoveg = Label(root, text="hey")
szoveg.pack()
button = Button(root, text= "fasz", command= show)
button.pack()
root.mainloop()
I tried text.pack_forget()
in a different function, but it wasn't defined. I don't know how to define it in another function.
答案1
得分: 1
以下是您要翻译的代码部分:
The question was a little hard to understand, but it sounds like what you want is the button to toggle the Label on and off. This can be accomplished in a few different ways, depending on whether you want the label to be declared globally or not.
One way of doing this is shown here:
from tkinter import *
root = Tk()
root.geometry("300x200")
def toggle():
try:
# Check to see if the root window has a child named "szoveg", and if so destroy it
root.nametowidget('.szoveg').destroy()
except:
# Otherwise, create a label named "szoveg"
szoveg = Label(root, text="hey", name="szoveg")
szoveg.pack()
button = Button(root, text= "button", command= toggle)
button.pack()
root.mainloop()
Every time you click the button, there is a check to see if the widget "szoveg" exists as a child of the root window. If there is one, remove it. If the `try` results in an error (aka there is no label named "szoveg"), then it will move on the the `except`, which creates the label.
Another way of doing this is just having the label created outside of the callback function, like so:
from tkinter import *
root = Tk()
root.geometry("300x200")
szoveg = Label(root, text="hey", name="szoveg")
def toggle():
if szoveg.winfo_ismapped():
szoveg.pack_forget()
else:
szoveg.pack()
button = Button(root, text= "button", command= toggle)
button.pack()
root.mainloop()
`szoveg.winfo_ismapped()` checks to see if the `pack` or `grid` methods have been called on it.
A handy thing about this method is that the label widget always exists, so if you need to do something else with it there won't be any errors.
Hope this helped,
Quizzer515SY
英文:
The question was a little hard to understand, but it sounds like what you want is the button to toggle the Label on and off. This can be accomplished in a few different ways, depending on whether you want the label to be declared globally or not.
One way of doing this is shown here:
from tkinter import *
root = Tk()
root.geometry( "300x200" )
def toggle():
try:
# Check to see if the root window has a child named "szoveg", and if so destroy it
root.nametowidget('.szoveg').destroy()
except:
# Otherwise, create a label named "szoveg"
szoveg = Label(root, text="hey", name="szoveg")
szoveg.pack()
button = Button(root, text= "button", command= toggle)
button.pack()
root.mainloop()
Every time you click the button, there is a check to see if the widget "szoveg" exists as a child of the root window. If there is one, remove it. If the try
results in an error (aka there is no label named "szoveg"), then it will move on the the except
, which creates the label.
Another way of doing this is just having the label created outside of the callback function, like so:
from tkinter import *
root = Tk()
root.geometry( "300x200" )
szoveg = Label(root, text="hey", name="szoveg")
def toggle():
if szoveg.winfo_ismapped():
szoveg.pack_forget()
else:
szoveg.pack()
button = Button(root, text= "button", command= toggle)
button.pack()
root.mainloop()
szoveg.winfo_ismapped()
checks to see if the pack
or grid
methods have been called on it.
A handy thing about this method is that the label widget always exists, so if you need to do something else with it there won't be any errors.
Hope this helped,
Quizzer515SY
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论