【问题标题】:How can i change content inside of a window after pressing a button in tkinter?在 tkinter 中按下按钮后,如何更改窗口内的内容?
【发布时间】:2022-09-23 06:17:41
【问题描述】:

我实际上找到了一个解决方案,但他们使用了一种完全不同的方式来制作窗口/按钮,我不知道如何使用它。 请告诉我是否有办法用我的代码实现这一点。 我的窗口的代码及其内容如下

from tkinter import *

title = \'zromber\'
window = Tk()
window.geometry(\"800x400\")

def play():
    print(\'welcome\')
    window.destroy()

def save():
    print(\'yes\')

playbutton = Button(window, text=\'play\')
playbutton.config(command=play)
playbutton.config(font=(\'none\', 50, \'bold\'))
testlabel = Label(window, text=title)
testlabel.config(font=(\'Ink Free\', 50))
testlabel.pack()
playbutton.pack()
savebutton = Button(window, text=\'save\')
savebutton.config(command=save)
savebutton.config(font=(\'none\', 50, \'bold\'))
savebutton.pack()
window.mainloop()

    标签: python tkinter button window


    【解决方案1】:
    from tkinter import *
    title = 'zromber'
    window = Tk()
    window.geometry("800x400")
    
    my_text = "Hi, I'm a the new label"
    
    
    def play():
        #use label.config(text="new text") to change text
       my_label.config(text=my_text+" and I'm from play")
    
    
    def save():
        my_label.config(text=my_text+" and I'm from save")
    
    
    playbutton = Button(window, text='play')
    playbutton.config(command=play)
    playbutton.config(font=('none', 50, 'bold'))
    testlabel = Label(window, text=title)
    testlabel.config(font=('Ink Free', 50))
    testlabel.pack()
    
    playbutton.pack()
    savebutton = Button(window, text='save')
    savebutton.config(command=save)
    savebutton.config(font=('none', 50, 'bold'))
    savebutton. pack()
    #Define the label
    my_label = Label(window, text="THIS IS A LABEL")
    my_label.config(font=('none', 10, 'bold'))
    my_label.pack()
    
    window.mainloop()
    

    在这里,我创建了一个名为 my_label 的新标签,当播放按钮调用 play() 函数时,我使用 label.config(text="new text") 更改文本。
    您可以运行上面的示例代码来获得结果。

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2021-09-25
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多