首页 文章

按钮点击后,Python tkinter在框架中显示文本

提问于
浏览
-2

作为一个Python新手,我试图用tkinter创建一个应用程序 . 我用一个下拉菜单制作了一个窗口 . 现在,我希望实现这一点,当您从下拉菜单中单击按钮时,文本将显示在右侧框架中或底部 . 接下来,当您按下下一个按钮时,屏幕上将显示其他文本(它将首先从之前单击的按钮中删除文本) .

你们有可能用一个窗口,一个按钮和一个框架给我一小段代码 . 一旦你点击按钮文字显示在框架中?

现在我有一些代码,一旦我点击一个按钮,就会打开一个带有文本的消息框 . 但我希望文本出现在框架中 . 我想只需几行代码即可给出一个例子,我不会在这里放置代码 .

提前致谢 .

1 回答

  • 0

    这样做是否有效:

    from tkinter import *
    
    def toggle ():
        global shown
        if shown: l.grid_remove () # Hide the text
        else: l.grid () # Show the text
        shown = not shown # Reverse the 'shown' boolean value
    
    # Create the window with all the widgets contained within a frame
    root = Tk ()
    f = Frame (root)
    f.grid ()
    shown = False
    Button (f, text = "Toggle text", command = toggle).grid ()
    l = Label (f, text = "Your text")
    root.mainloop ()
    

    否则,这样做:

    from tkinter import *
    
    LIST = ["Something", "Something else"]
    
    root = Tk ()
    value = StringVar ()
    value.set (LIST [0])
    OptionMenu (root, value, *LIST).grid ()
    l = Label (root, text = LIST [0])
    l.grid ()
    value.trace ("w", lambda a, b, c: l.config (text = value.get ()))
    root.mainloop ()
    

相关问题