gpt4 book ai didi

python - 在 Tk 小部件中显示标准输出

转载 作者:行者123 更新时间:2023-12-01 05:32:03 26 4
gpt4 key购买 nike

我已经完成了这个工作,但是我怎样才能让输出显示在 GUI 中???我不明白这个问题,我想知道是否有人可以帮助我

最佳答案

正如 Ashish 评论的那样,只需使用 Label 并将其绑定(bind)到 StringVar 变量即可:

class Application(Frame):


def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()

# The class below will house the buttons and boxes
def create_widgets(self):
self.button1 = Button(self, text = "Four Sided Dice", command=self.dice4)
self.button1.grid()

self.button2 = Button(self, text = "Six Sided Dice", command=self.dice6)
self.button2.grid()

self.button3 = Button(self, text = "Twelve Sided Dice", command=self.dice12)
self.button3.grid()

self.label_var = StringVar()

self.label1 = Label(self, textvariable=self.label_var)
self.label1.grid()

# The bellow classes define the commands to use the random function
def dice4(self):
fourside = random.randint(1,4)
self.label_var.set("You Rolled a " + str(fourside) + " using a four sided dice")

def dice6(self):
sixside = random.randint(1,6)
self.label_var.set("You Rolled a " + str(sixside) + " using a six sided dice")

def dice12(self):
twelveside = random.randint(1,12)
self.label_var.set("You Rolled a " + str(twelveside) + " using a twelve sided dice")


app = Application(diceroll)

diceroll.mainloop()

注意:我制作了 Application 类的 dice 函数方法。

关于python - 在 Tk 小部件中显示标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com