Home > other >  How to make a tkinter gui and display printed value
How to make a tkinter gui and display printed value

Time:02-11

This is how far I came.... I got the winner know I want to display it on the gui here is my code: I tried many google searches and found none I hoped that someone may help me

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.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 = runn)
        run.pack(pady= 5, padx = 5)

def runn():
    PEOPLE = [
  'Juan',
  'Owen'
]

    print("\n\n")
    bar = Bar('And the winner is...', max=2)
    for i in range(2):
        [x for x in range(999999)]  # short pause...
        bar.next()
    bar.finish()
    print("\n\n\t{}!\n\n".format(random.choice(PEOPLE)))
    print("-" * 60   "\n")

app = Application()
app.mainloop()

CodePudding user response:

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()

CodePudding user response:

I recommend you to read more in the Tkinter docs: https://docs.python.org/3/library/tk.html

You should also start using the grid GUI and not the stack GUI. The grid version is newer and easier to learn and modify.

It should be enough for you to import only:
from tkinter import *
from tkinter.ttk import *.

Here is a full description how to make a progressbar from Stackoverflow: tkinter gui with progress bar

There is many ways to display your code: showbox'es, labels, textboxes etc. So read through the docs and find the one that fits your widget.

CodePudding user response:

You can use tk.Label to replace the print() output, ttk.Progressbar to replace the console progress bar and use .after() to update the progress bar:

import tkinter as tk
from tkinter import ttk
import random

backgroundcolor = '#C46C6C'

class Application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry('500x500')
        self.title('TheXoGenie')
        self.configure(bg=backgroundcolor)

        title = tk.Label(self, text="Welcome to TheXoGenie", font='Helvetica 18 bold', bg=backgroundcolor)
        title.pack(pady=2, padx=2)

        run = tk.Button(self, text="Run", command=self.runn)
        run.pack(pady=5, padx=5)

        self.msg = tk.Label(self, text='And the winner is...', font='Helvetica 16 bold', bg=backgroundcolor)
        self.bar = ttk.Progressbar(self, orient='horizontal', length=200)
        self.winner = tk.Label(self, font='Helvetica 32 bold', bg=backgroundcolor)

    def runn(self):
        PEOPLE = ['Juan', 'Owen']

        # hide the previous winner
        self.winner.pack_forget()

        # show the message and progress bar
        self.msg.pack(pady=10)
        self.bar.pack()

        def show_winner(n=0):
            # update progress bar
            self.bar['value'] = n
            if n > 100:
                # hide the progress bar
                self.bar.pack_forget()
                # show the winner
                self.winner.config(text=random.choice(PEOPLE))
                self.winner.pack(pady=10)
            else:
                # continue the progress bar animation
                self.after(300, show_winner, n 20)

        show_winner() # start progress bar animation and then show the winner

app = Application()
app.mainloop()
  • Related