我用tkinter编写了一个python程序,以显示用户单击“输入”按钮时这四个国家(尼日利亚,加拿大,中国和澳大利亚)的大陆。我想要当用户单击“输入”按钮时,响应标签中的文本应显示所选国家/地区所属的特定大陆。
主要问题是我不知道如何在主程序窗口中的可见标签中更新输出,而是将输出显示在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
答案 0 :(得分:0)
您快到了。您需要使用Entry小部件。
from functools import partial
import tkinter as tk
from tkinter import *
def serial_port(var,entry): #add the Entry widget as variable
entry.delete(0, 'end') #clear the entry
selection = var.get()
text_dict = {
1: 'Africa',
2: 'North America',
3: 'Asia',
4: 'Australia'}
text_to_print = text_dict[selection]
entry.insert(0,text_to_print) #set the entry
def main():
root= tk.Tk()
root.title("Continent")
root.geometry("500x300")
var = IntVar()
var.set(1)
entry = Entry() #create the Entry widget
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)
Enter_Button=Button(root, text='ENTER',command=partial(serial_port, var, entry), relief="ridge", background="Cyan", width=20) #pass the Entry widget
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)
entry.grid(row=8, column=3) #place the Entry widget
root.mainloop()
if __name__ == '__main__':
main()
答案 1 :(得分:0)
要将文本打印到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()
答案 2 :(得分:0)
使用Label()
函数。它需要父窗口小部件和文本。
Label(parent, text = "what you want")
。