Something you could do is integrate the runn function into the class and update class variables (as in the example below, self.winner as the winner and self.display_winner as the label you want to show);
import tkinter as tk
import random
from progress.bar import Bar
backroundcolor = '#C46C6C'
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.winner = None
self.display_winner = None
self.geometry('500x500')
self.title('TheXoGenie')
self.configure(bg=backroundcolor)
title = tk.Label(self, text="Welcome to TheXoGenie", font='Helvetica 18 bold', bg=backroundcolor)
title.pack(pady= 2, padx= 2)
run = tk.Button(self, text ="Run", command = self.runn)
run.pack(pady= 5, padx = 5)
def runn(self):
PEOPLE = [
'Juan',
'Owen'
]
self.winner = random.choice(PEOPLE)
if self.display_winner:
self.display_winner.destroy()
self.display_winner = tk.Label(self, text="The winner is " + self.winner + "!!!", font='Helvetica 32 bold', bg=backroundcolor)
self.display_winner.pack(pady= 10, padx= 2)
app = Application()
app.mainloop()
We check if we already have displayed a winner, if so we delete the label and make a new one. If not we just add the new label with the winner. If we do not check this, the labels will keep stacking in the window.
If you want to check the new winner against the old one you just add another variable and check if the new winner matches the old;
import tkinter as tk
import random
from progress.bar import Bar
backroundcolor = '#C46C6C'
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.winner = None
self.display_winner = None
self.old_winner = None
self.geometry('500x500')
self.title('TheXoGenie')
self.configure(bg=backroundcolor)
title = tk.Label(self, text="Welcome to TheXoGenie", font='Helvetica 18 bold', bg=backroundcolor)
title.pack(pady= 2, padx= 2)
run = tk.Button(self, text ="Run", command = self.runn)
run.pack(pady= 5, padx = 5)
def runn(self):
PEOPLE = [
'Juan',
'Owen'
]
self.winner = random.choice(PEOPLE)
if self.display_winner:
self.display_winner.destroy()
if self.winner == self.old_winner:
self.display_winner = tk.Label(self, text="The winner is " + self.winner + " again!!!", font='Helvetica 24 bold', bg=backroundcolor)
else:
self.display_winner = tk.Label(self, text="The winner is " + self.winner + "!!!", font='Helvetica 24 bold', bg=backroundcolor)
self.display_winner.pack(pady= 10, padx= 2)
self.old_winner = self.winner
app = Application()
app.mainloop()