简体   繁体   English

将Tkinter框架放置在另一个框架中

[英]Placing Tkinter Frame in another Frame

I am trying to experiment and get the button to only display the label when the button is clicked instead it is opening up another GUI window. 我正在尝试进行实验,并使该按钮仅在单击按钮时显示标签,而是打开另一个GUI窗口。 The main frame is called secret message. 主机称为秘密消息。 Within this when i click onto the button it should then replace the empty place with the label in row=2 . 在此范围内,当我单击按钮时,应该用row=2的标签替换空白处。

Could someone explain to me how i can raise the label rather than just opening up a new window. 有人可以向我解释如何提高标签,而不仅仅是打开一个新窗口。 All code is functional but i want another way around this, i am new to python. 所有代码都可以正常运行,但是我想采用另一种方法,我是python的新手。

from tkinter import *

def topLevel():
    top=Toplevel()
    top.geometry("300x200")

root=Tk()
root.title("Secret Message")

button1 = Button(text="Push this button to see hidden message!", width =60, command=topLevel)
button1.grid(row=1, column=0)    

label1 = Label(width=50, height=10, background="WHITE", text= "There is no secret!")
label1.grid(row=2, column=0)

root.mainloop()

You question title has nothing to do with your question. 您的问题标题与您的问题无关。

To update the geometry of your label you simple need to tell the function where you want the label on the container you set up your label in. In this case you do not define the container so the widgets default to the root window. 要更新标签的几何形状,只需告诉函数要将标签放置在容器上的容器上的位置。在这种情况下,您无需定义容器,因此小部件默认为根窗口。

Here is a working example that will update the label geometry when you press the button. 这是一个有效的示例,当您按下按钮时,它将更新标签的几何形状。

from tkinter import *


root=Tk()
root.title("Secret Message")

def grid_label():
    label1.config(text="There is no secret!")

Button(root, text="Push this button to see hidden message!", width=60, command=grid_label).grid(row=1, column=0)    
label1 = Label(root, width=50, height=10, background="WHITE")
label1.grid(row=2, column=0)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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