简体   繁体   中英

How to refresh the window in Tkinter?

I've been writing this program using the Tkinter module in Python3.6.2. There are buttons to to open a list of teams and a list of competitors and I'd prefer to keep the outputs in one window, however if I click the "Teams" button followed by the "Individuals" button, the two elements open one after the other.

I'm hoping to add a button that resets the window (removing the outputs, leaving just the buttons), but I haven't found any solutions online that work with my program.

from tkinter import *
import tkinter as tk

bgcol = "#0B8CFF"

class MainMenu:
    def __init__(self, master):

        self.master = master
        master.title("Scoring System GUI")
        master.geometry("500x750+75+60")

        self.label = Label(master, text="GUI")
        self.label.pack()

        self.team_button = Button(master, text="Teams", command=self.openTeams)
        self.team_button.pack()

    def openTeams(self):        
        self.label = Label(text="Team #1:")
        self.label.pack()
        team1 = open("team1.csv", "r")
        message = team1.read()
        text = Text(root, width = "50", height = "6", fg = "#000000")
        text.pack()
        text.insert(END, message)
        redteam.close()

Here's a photo of the current output:

Tkinter GUI

You have to clear the Text widget before inserting new strings. Just insert the following line before your text.insert(...) statement:

text.delete('1.0', END)

By the way, if you only want to display a list and not edit it (as I guess is the case here) a Listbox widget if often a better choice than a Text widget.

To do that you need to create another method inside your class and parse it to another button as callback command.

def reset_func(self):
    self.text.delete('1.0', END)

with this method inside when you parse it as command to your button it will clear the content in text widget .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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