隐藏和取消隐藏没有网格的按钮的方法?(Python特金特)

2024-05-08 15:17:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有一种方法可以在python中隐藏和取消隐藏按钮而不使用grid。我曾经尝试过将它们移动到屏幕外的位置,然后在它被变量触发后再返回,但这不起作用(这使得按钮看起来好像从未存在过)。包装,忘记,然后拆箱似乎只与标签工作。如果有人有别的办法,请告诉我(注:我是新手)。以下是我目前的代码:

from tkinter import*
health=1
stamina=1
magica=1
healthcap=1
staminacap=1
magicacap=1
slide=1

def next1(event=None):
    global slide
    if slide==1:
        bglabel.config(image=bg1)
    elif slide==2:
        classstart.pack()
        classstart.pack_forget()
        bglabel.config(image=bg2)
    elif slide==3:
        classstart.pack()
        bglabel.pack()
        bglabel.pack_forget()
        nextbutton1.pack()
        nextbutton1.pack_forget()
    slide+=1
    window.update()

def class_select_screen():
    global slide
    if slide==3:
        classstart.config(image=classdesc)
        rougeselbut.pack()
        mageselbut.pack()
        warriorselbut.pack()
    elif slide<3:
        rougeselbut.pack()
        rougeselbut.pack_forget()
        mageselbut.pack()
        mageselbut.pack_forget()
        warriorselbut.pack()
        warriorselbut.pack_forget()

window = Tk()
window.geometry("1500x750+0+0")

classdesc=PhotoImage(file="classdescription.png")
rougedesc=PhotoImage(file="rougedescription.png")
warriordesc=PhotoImage(file="warriordescription.png")
magedesc=PhotoImage(file="magedescription.png")
bg1=PhotoImage(file="backslide1.png")
bg2=PhotoImage(file="backslide2.png")
nextbutton=PhotoImage(file="next.png")
magesel=PhotoImage(file= "mageselect.png")
warriorsel=PhotoImage(file="warriorselect.png")
rougesel=PhotoImage(file="rougeselect.png")

classlabel=Label(window)
classstart= Label(window, image=classdesc)
classstart.place(x=600, y=200)
bglabel= Label(window, image=bg1)
bglabel.place(x=600,y=200)

rougeselbut= Button(window, image=rougesel, bd=0, command=rouge_select())
rougeselbut.place(x=500, y=200)

warriorselbut= Button(window, image=warriorsel, bd=0, command= warrior_select())
warriorselbut.place(x=670, y=195)

mageselbut= Button(window, image=magesel, bd=0, command=mage_select())
mageselbut.place(x=840, y=200)

nextbutton1=Button(window, image=nextbutton, bd=0, command=next1)
nextbutton1.place(x=100, y=100)

window.bind('<Button-1>', lambda event: next1(event))
window.bind('<Button-1>', lambda event: rouge_select(event))
window.bind('<Button-1>', lambda event: warrior_select(event))
window.bind('<Button-1>', lambda event: mage_select(event))
window.mainloop()
  1. 按钮mageselbut、warriorselbut和rougeselbut还没有做很多事情,但是它们应该在slide=3之前是不可见的,当然不是,这使得class\u select\u screen现在非常无用(有没有办法解决这个问题?)。你知道吗

classstart标签能够很好地恢复,我想知道为什么。一开始我认为可能只是因为它不在next1内,但我尝试了两种不同的方法,一种是它完全失败,就像其他方法一样,另一种是当slide=2时它是不可见的,但不是1。你知道吗

  1. “下一步”需要单击两次才能使幻灯片等于2,我不太明白。它以1开头,结尾是slide+=1,所以它应该在一次单击中变为slide=2,对吗?你知道吗

所以是的。有很多,但如果有人有想法请告诉我。我现在不想使用网格,因为我还不太熟悉它,而且我读过的解释都没有意义,所以我打算在周一向我的教授询问。(如果有人能在这里解释一下,那就太棒了,尽管我知道我要求很多)

对不起,如果我不清楚的话,请提前谢谢。你知道吗


Tags: imageeventpngbuttonwindowselectpackfile
1条回答
网友
1楼 · 发布于 2024-05-08 15:17:52

要隐藏由pack manager显示的按钮b1,请使用^{}。要再次显示它,可以像以前从未显示过一样:使用b1.pack()b1.place()或(如果屏幕上已经没有其他内容了)b1.grid()。你知道吗

注意,b1将打包在底部(在所有其他已经打包的小部件之后)。为了避免这种情况,我建议使用不同的布局管理器(gridplace),或者,当您想再次显示b1时,隐藏所有应该在b1下面的小部件,然后按正确的顺序打包b1。你知道吗

由于缺少函数,我无法正确运行您的代码,因此无法判断是否有问题并为您进行调试。你知道吗

既然您是新手,我建议您使用两个tkinter参考网站,以便更轻松地找到问题的答案,并扩展您对tkinter的了解: effbot.org&;infohost.nmt.edu

在那里你可以详细了解网格是如何工作的。简而言之,网格有行和列。当被网格化时,每个小部件被放置在它自己的单元格中(第i行,第j列)。默认情况下,每行/每列的高度/宽度与内部最高/最宽的小部件相同。这意味着,如果某一行/列中没有任何小部件,则不会显示该行/列。当然,你可以用网格选项来调整它(例如row_configure()可能会有帮助),更多信息请查看我链接的站点。你知道吗

相关问题 更多 >