如何暂时隐藏标签文本,以便它不会覆盖整个tkinter窗口

huangapple go评论104阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年8月10日 22:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定