简体   繁体   English

在Tkinter中清除屏幕

[英]Clearing the screen in Tkinter

I have tried all the other posts on this topic but none of them have worked for me... 我已经尝试过有关该主题的所有其他帖子,但没有一个对我有用。

Here is my code: 这是我的代码:

from tkinter import *

window=Tk()
window.geometry('600x400')
window.title('hello world')

def wrong():
    root=Tk()
    text=Text(root)
    text.insert(INSERT,"WRONG!, you stupid idiot!!!!")
    text.pack()
def right():
    root=Tk()
    text=Text(root)
    text.insert(INSERT,"CORRECT, good job!")
    text.pack()
def reset():
    hide_widgets()
class UIProgram():
    def setupUI(self):
        buttonlist=[]
        button= Button(window,text='Sanjam',command=wrong).pack()
        button2=Button(window,text='Sunny the Bunny',command=wrong).pack()
        button3= Button(window, text='Sunjum',command=right).pack()
        button4= Button(window, text='bob',command=wrong).pack()
        button5= Button(window, text='next',command=reset)
        button5.pack()
        self.label=Label(window)
        self.label.pack()
        window.mainloop()
program= UIProgram()
program.setupUI()

I am aware of pack_forget() and tried it, but it keeps giving me an error. 我知道pack_forget()并尝试了它,但它一直给我一个错误。 Also, is it possible to make a command(like the 'reset' one I have) and use that in the command for a clear screen button. 另外,是否可以制作一条命令(如我拥有的“重置”命令),并将其用于清晰的屏幕按钮。 Please help, I am new to Tkinter and don't know much about these things.. 请帮助,我是Tkinter的新手,对这些东西了解不多。

Thanks 谢谢

Example code 范例程式码

I'm tired to describe the same mistakes hundreds of times - some comments in code. 我厌倦了数百次描述相同的错误-代码中有一些注释。

import tkinter as tk

# --- classes ---

class UIProgram():

    def __init__(self, master):
        self.master = master # use to add elements directly to main window

        self.buttons = [] # keep buttons to change text

        # frame to group buttons and easily remove all buttons (except `Next`)
        self.frame = tk.Frame(self. master)
        self.frame.pack()

        # group button in frame
        button = tk.Button(self.frame, text='Sanjam', command=self.wrong)
        button.pack()
        self.buttons.append(button)

        button = tk.Button(self.frame, text='Sunny the Bunny', command=self.wrong)
        button.pack()
        self.buttons.append(button)

        button = tk.Button(self.frame, text='Sunjum', command=self.right)
        button.pack()
        self.buttons.append(button)

        button = tk.Button(self.frame, text='bob', command=self.wrong)
        button.pack()
        self.buttons.append(button)

        # button outside frame
        button_next = tk.Button(self.master, text='Next >>', command=self.reset)
        button_next.pack()

        self.label = tk.Label(self.frame)
        self.label.pack()


    def wrong(self):
        # create second window with message and closing button
        win = tk.Toplevel()
        tk.Label(win, text="WRONG!, you stupid idiot!!!!").pack()
        tk.Button(win, text='close', command=win.destroy).pack()

    def right(self):
        # create second window with message and closing button
        win = tk.Toplevel()
        tk.Label(win, text="CORRECT, good job!").pack()
        tk.Button(win, text='close', command=win.destroy).pack()

    def reset(self):
        # remove frame with all buttons
        self.frame.pack_forget()
        tk.Label(self.master, text="frame removed").pack()

        # or only remove text in labels
        #for button in self.buttons:
        #    button['text'] = '-- place for new text --'

# --- main ---

root = tk.Tk()
root.geometry('600x400')
root.title('hello world')

program = UIProgram(root)

root.mainloop()

BTW: if you do var = Widget(...).pack() then you assign None to var because pack()/grid()/place() return None . 顺便说一句:如果您执行var = Widget(...).pack()则您将None分配给var因为pack()/grid()/place()返回None You have to do it in two lines 您必须分两行进行

var = Widget(...)
var.pack()

or in one line if you don't need var 或一行,如果您不需要var

Widget(...).pack()

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

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