【问题标题】:How to make my output appear in tkinter GUI instead of shell? [duplicate]如何让我的输出出现在 tkinter GUI 而不是 shell 中? [复制]
【发布时间】:2019-12-18 01:30:19
【问题描述】:

我对编码很陌生,所以如果我的代码看起来像垃圾(可能确实如此),请耐心等待。我只希望我的输出出现在我制作的 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()

【问题讨论】:

  • 删除所有 print(...) 的调用 text=print(...),例如text=random.choice(age).

标签: python user-interface tkinter window output


【解决方案1】:

您的代码中有几个问题。您所说的问题是由于在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()

【讨论】:

    【解决方案2】:

    我从未使用过 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))
    

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

    【讨论】:

    • 如果这个答案是正确的,我会推荐它并将其标记为接受:)
    【解决方案3】:

    您不能使用 print 输出到 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()
    

    How to format your Python code

    How to use Text() in depth

    How to use Label() in depth

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2018-12-13
      • 2019-08-31
      • 1970-01-01
      • 2011-06-25
      相关资源
      最近更新 更多