如何制作 tkinter gui 并显示打印值

How to make a tkinter gui and display printed value

这就是我的进展....我让获胜者知道我想在 gui 上显示它这是我的代码: 我尝试了很多 google 搜索并找到了 none 我希望有人可以帮助我

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

您可以将 runn 函数集成到 class 中并更新 class 变量(如下例所示,self.winner 作为获胜者,self.display_winner作为您要显示的标签);

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

我们检查是否已经显示了获胜者,如果是,我们删除标签并创建一个新标签。如果不是,我们只是添加带有获胜者的新标签。如果我们不检查这个,标签将继续堆叠在 window.

如果您想将新赢家与旧赢家进行比较,您只需添加另一个变量并检查新赢家是否与旧赢家相匹配;

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

我建议您阅读更多 Tkinter 文档:https://docs.python.org/3/library/tk.html

您还应该开始使用网格 GUI 而不是堆栈 GUI。 grid版本更新更容易学习和修改

你只需要导入就够了:
from tkinter import *
from tkinter.ttk import *.

以下是如何从 Whosebug 制作进度条的完整说明:

显示代码的方法有很多种:显示框、标签、文本框等。因此请通读文档并找到适合您的小部件的方法。

可以使用tk.Label替换print()输出,ttk.Progressbar替换控制台进度条,使用.after()更新进度条:

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