gpt4 book ai didi

python - 如何使 python tkinter 输出显示在 GUI 而不是 python shell 中?

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:44 24 4
gpt4 key购买 nike

我用 tkinter 编写了一个 python 程序,当用户单击“输入”按钮时,可以显示这 4 个国家/地区的大陆:尼日利亚、加拿大、中国和澳大利亚。我希望当用户单击“Enter”按钮时,响应标签中的文本应显示所选国家/地区所属的特定大陆。

主要问题是我不知道如何使输出在主程序窗口中可见的标签中更新,而是将输出显示在 python shell 上。 (我是 python tkinter GUI 的新手)

============================== 代码 ================= ===================

from functools import partial
import tkinter as tk
from tkinter import *

def serial_port(var):
selection = var.get()
# to save space
text_dict = {
1: 'Africa',
2: 'North America',
3: 'Asia',
4: 'Australia'}
text_to_print = text_dict[selection]
print(text_to_print) #apparently, I learn I can't use print in GUI but idk what to use in place of print here


def main():
root= tk.Tk()
root.title("Continent")
root.geometry("500x300")
var = IntVar()
var.set(1)
#Button to show selected profile to assign FTW
Lang_1=Radiobutton(root, text='Nigeria', variable=var, value=1, width=20)
Lang_2=Radiobutton(root, text='Canada', variable=var, value=2, width=20)
Lang_3=Radiobutton(root, text='Japan', variable=var, value=3, width=20)
Lang_4=Radiobutton(root, text='Australia', variable=var, value=4, width=20)
# Button to show entered reg values and data in it
Enter_Button=Button(root, text='ENTER',command=partial(serial_port, var), relief="ridge", background="Cyan", width=20)

Lang_1.grid(row=1, column=5)
Lang_2.grid(row=2, column=5)
Lang_3.grid(row=3, column=5)
Lang_4.grid(row=4, column=5)
Enter_Button.grid(row=7, column=3)
root.mainloop()

if __name__ == '__main__':
main

最佳答案

要将文本打印到 tkinter GUI,您需要一个单独的标签小部件。

检查下面的代码。我添加了小部件 Return_Label,您可以根据需要更改其配置和位置。我很确定这应该符合您的要求。

#These variables are made global so both functions can access them
var = None
Return_Label = None


def serial_port():
global var, Return_Label

selection = var.get()
text_dict = {
1: "Africa",
2: "North America",
3: "Asia",
4: "Australia"}
text_to_print = text_dict[selection]
Return_Label.config(text = text_to_print)



def main():
global var, Return_Label

root = tk.Tk()
root.title("Continent")
root.geometry("500x300")
var = tk.IntVar()
var.set(1)

Lang_1 = tk.Radiobutton(root, text = "Nigeria", variable = var, value = 1, width = 20)
Lang_2 = tk.Radiobutton(root, text = "Canada", variable = var, value = 2, width = 20)
Lang_3 = tk.Radiobutton(root, text = "Japan", variable = var, value = 3, width = 20)
Lang_4 = tk.Radiobutton(root, text = "Australia", variable = var, value = 4, width = 20)

Enter_Button = tk.Button(root, text = "ENTER", command = serial_port, relief = "ridge", bg = "Cyan", width = 20)

Return_Label = tk.Label(root)

Lang_1.grid(row = 1, column = 5)
Lang_2.grid(row = 2, column = 5)
Lang_3.grid(row = 3, column = 5)
Lang_4.grid(row = 4, column = 5)
Enter_Button.grid(row = 7, column = 3)
Return_Label.grid(row = 6, column = 3) #change this to wherever you want it printed
root.mainloop()

if (__name__ == "__main__"):
main()

关于python - 如何使 python tkinter 输出显示在 GUI 而不是 python shell 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55288986/

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