简体   繁体   中英

How do I make my python tkinter output to show in GUI instead of python shell?

I wrote a python program with tkinter to reveal the continent of each these 4 countries: Nigeria, Canada, China, and Australia -- when the user clicks an "enter" button. I want when the user clicks the "Enter" button, then the text in the response label should reveal the particular continent the selected country belongs to.

The main issue is that I don't know how to make the output to update in a label visible in the main program window, instead the output is displayed on python shell. (I'm a novice in python tkinter GUI)

============================ The Code =====================================

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

You're almost there. You need to use an Entry widget.

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()

To print text into a tkinter GUI, you need a separate label widget.

Check the code below. I have added the widget Return_Label , which you can change the config and position of to however you like. I'm pretty sure this should do what you want.

#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()

use the Label() function. It takes the parent widget, and the text.

Label(parent, text = "what you want") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM