如何让我的输出出现在 tkinter GUI 而不是 shell 中?



我对编码很陌生,所以如果我的代码看起来像垃圾,请耐心等待(可能确实如此。我只是希望我的输出出现在我制作的 GUI 中,而不是在 shell 中。如何修改我的代码以实现此目的?

import random
from tkinter import *
# Attributes
age = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96",
"97", "98", "99", "100", ]
country = ["United States", "Brazil", "Mexico", "China", "Japan", "Canada", "France", "Germany"]
male_name = ["Joe", "Eden", "Diego", "Anthony", "Jarod", "Kique", "Austin", "Hunter"]
female_name = ["Haley", "Ariana", "Sarah", "Jackie", "Serena"]
gender_male = "Male"
gender_female = "Female"
# Random Generation
def print_start_life(event):
Text(window, text=print(random.choice(age)), font=("Arial Bold", 16))
Text(window, text=print(random.choice(country)), font=("Arial Bold", 16))
Text(window, text=print(random.choice(male_name or female_name)), font=("Arial Bold", 16))
if male_name:
Text(window, text=print(gender_male), font=("Arial Bold", 16))
elif female_name:
Text(window, text=print(gender_female), font=("Arial Bold", 16))

# GUI
window = Tk()
window.title("Random Life")
window.geometry('800x500')
lbl = Label(window, text="Do you want to play Random Life?", font=("Arial Bold", 25))
lbl.grid(column=0, row=0)
btn = Button(window, text="Yes")
btn.bind("<Button-1>", print_start_life)
btn.grid(column=1, row=0)
window.mainloop()

代码中存在几个问题。 您所说的问题是由于在text=print(...)中呼叫print(...)。 你不需要调用print(...),只需将print(...)的参数分配给text就足够了。

此外,您不应该在每次单击Yes按钮时重新创建Text小部件。 您应该创建Label小部件以显示一次随机结果,然后在print_start_life()函数中更改其文本。

下面是代码的修改版本:

import random
from tkinter import *
# Attributes
age = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96",
"97", "98", "99", "100", ]
country = ["United States", "Brazil", "Mexico", "China", "Japan", "Canada", "France", "Germany"]
male_name = ["Joe", "Eden", "Diego", "Anthony", "Jarod", "Kique", "Austin", "Hunter"]
female_name = ["Haley", "Ariana", "Sarah", "Jackie", "Serena"]
gender_male = "Male"
gender_female = "Female"
# Random Generation
def print_start_life(event=None):
selected_age.config(text=random.choice(age))
selected_country.config(text=random.choice(country))
name = random.choice(male_name+female_name)
selected_name.config(text=name)
gender.config(text=gender_male if name in male_name else gender_female)
# GUI
window = Tk()
window.title("Random Life")
window.geometry('800x500')
lbl = Label(window, text="Do you want to play Random Life?", font=("Arial Bold", 25))
lbl.grid(column=0, row=0)
btn = Button(window, text="Yes", command=print_start_life)
#btn.bind("<Button-1>", print_start_life)
btn.grid(column=1, row=0)
frm = Frame(window)
font1 = ("Arial", 16)
font2 = ("Arial Bold", 16)
Label(frm, text='Age:', font=font1).grid(row=0, column=0, sticky=E)
selected_age = Label(frm, font=font2)
selected_age.grid(row=0, column=1, sticky=W)
Label(frm, text='Country:', font=font1).grid(row=1, column=0, sticky=E)
selected_country = Label(frm, font=font2)
selected_country.grid(row=1, column=1, sticky=W)
Label(frm, text='Name:', font=font1).grid(row=2, column=0, sticky=E)
selected_name = Label(frm, font=font2)
selected_name.grid(row=2, column=1, sticky=W)
Label(frm, text="Gender:", font=font1).grid(row=3, column=0, sticky=E)
gender = Label(frm, font=font2)
gender.grid(row=3, column=1, sticky=W)
frm.grid(row=1, sticky=W)
window.mainloop()

我从未使用过tkinter,但我很确定这就是答案:

def print_start_life(event):
Text(window, text=str(random.choice(age)), font=("Arial Bold", 16))
Text(window, text=str(random.choice(country)), font=("Arial Bold", 16))
Text(window, text=str(random.choice(male_name or female_name)), font=("Arial Bold", 16))
if male_name:
Text(window, text=str(gender_male), font=("Arial Bold", 16))
elif female_name:
Text(window, text=str(gender_female), font=("Arial Bold", 16))

在我看来,您的文本对象只是调用打印函数,而不是为文本对象分配字符串。

不能使用打印输出到 Tkinter GUI。

您还使用的 Text(( 正在创建一个文本框,其中包含可以通过多种方式配置并在 GUI 中进行编辑的内容,我认为您更想只输出结果。无论哪种方式,我都对 Text(( 进行了编码以工作,并使用 Label(( 输出不可编辑版本的替代方法。但是,您必须找到一种方法来使用 Label 每次清除以前的输出,正如您在输出中看到它们相互堆叠一样。

我只写了年龄和国家,以节省一点时间

import random
from tkinter import *
# Attributes
age = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96",
"97", "98", "99", "100", ]
country = ["United States", "Brazil", "Mexico", "China", "Japan", "Canada", "France", "Germany"]
male_name = ["Joe", "Eden", "Diego", "Anthony", "Jarod", "Kique", "Austin", "Hunter"]
female_name = ["Haley", "Ariana", "Sarah", "Jackie", "Serena"]
gender_male = "Male"
gender_female = "Female"
# Random Generation
def print_start_life(event):
age_display = Text(window, font=("Arial Bold", 16), width = 2, height = 1)
age_display.insert(INSERT, str(random.choice(age)))
age_display.grid(column = 0, row = 1)
country_display = Label(window, text=str(random.choice(country)), font=("Arial Bold", 16))
country_display.grid(column = 0, row = 2)

# GUI
window = Tk()
window.title("Random Life")
window.geometry('800x500')
lbl = Label(window, text="Do you want to play Random Life?", font=("Arial Bold", 25))
lbl.grid(column=0, row=0)
btn = Button(window, text="Yes")
btn.bind("<Button-1>", print_start_life)
btn.grid(column=1, row=0)
window.mainloop()

如何格式化你的 Python 代码

如何深入使用 Text((

如何深入使用 Label((

最新更新