简体   繁体   中英

How to show an input on GUI tkinter

So I just want to make a simple table where the user inputs values for the mean, min, and max. This will then be displayed on GUI of Tkinter. I think I am almost there, but for some reason, I cannot seem to edit the label's I created.

from tkinter import * root = Tk() root.title("Simple Calculator")

out_mean = Label(root, text="Mean") 
out_min = Label(root, text="Min") 
out_max = Label(root, text="Max") #shows as text in the window

e_mean = Entry(root,width= 35, borderwidth = 5) 
e_min = Entry(root,width= 35, borderwidth = 5) 
e_max = Entry(root,width= 35, borderwidth = 5)

def click_mean(number):
    out_mean.delete(0, END)
    out_mean.insert(0, e_mean)

def click_min(number):
    out_min.delete(0, END)
    out_min.insert(0, e_min)

def click_max(number):
    out_max.delete(0, END)
    out_max.insert(0, e_max)

button_mean = Button(root, text = "mean", padx = 40, pady = 20, command = lambda: click_mean())  
button_min = Button(root, text = "min", padx = 40, pady = 20, command = lambda: click_min()) 
button_max = Button(root, text = "max", padx = 40, pady = 20, command = lambda: click_max())

e_mean.grid(row=0,column=0) 
e_min.grid(row=1,column=0) 
e_max.grid(row=2,column=0)

button_mean.grid(row=0,column=1) 
button_min.grid(row=1,column=1) 
button_max.grid(row=2,column=1)

out_mean.grid(row=0,column=2, columnspan = 2) 
out_min.grid(row=1,column=2, columnspan = 2) 
out_max.grid(row=2,column=2, columnspan = 2)

A couple of issues with your code.

Firstly, your button command is not correct. You shouldn't need to use a lambda when you have already defined a function that needs no input. Your lambda functions are also not calling the correct function. At no point do you call any of click_mean , click_min or click_max .

Your second error is passing the Entry as an argument to alter the text, not the text entered into the entry. You need to call e_mean.get() to get the string value.

Your third error is that you are trying to edit the text values of the Labels as if they are Entries. The correct call is <Label_name>.configure(text=e_mean.get()) .

Fixing all of these errors, your working code is now:

from tkinter import * 
root = Tk() 
root.title("Simple Calculator")

out_mean = Label(root, text="Mean") 
out_min = Label(root, text="Min") 
out_max = Label(root, text="Max") #shows as text in the window

e_mean = Entry(root,width= 35, borderwidth = 5) 
e_min = Entry(root,width= 35, borderwidth = 5) 
e_max = Entry(root,width= 35, borderwidth = 5)

def click_mean():
    out_mean.configure(text=e_mean.get())
def click_min():
    out_min.configure(text=e_min.get())
def click_max():
    out_max.configure(text=e_max.get())

button_mean = Button(root, text = "mean", padx = 40, pady = 20, command = click_mean)  
button_min = Button(root, text = "min", padx = 40, pady = 20, command = click_min) 
button_max = Button(root, text = "max", padx = 40, pady = 20, command = click_max)

e_mean.grid(row=0,column=0) 
e_min.grid(row=1,column=0) 
e_max.grid(row=2,column=0)

button_mean.grid(row=0,column=1) 
button_min.grid(row=1,column=1) 
button_max.grid(row=2,column=1)

out_mean.grid(row=0,column=2, columnspan = 2) 
out_min.grid(row=1,column=2, columnspan = 2) 
out_max.grid(row=2,column=2, columnspan = 2)

root.mainloop()

While not an error that you will be noticing (because tkinter is smart and can make do without) is that you aren't calling root.mainloop() at the end of your script. While not causing any errors currently, it is good practice to do so and can cause errors in some cases. You should also not use a wildcard import for tkinter ( from tkinter import * ). Instead explicitly define the import, import tkinter as tk or similar.

You changed up some of the things:

  1. the lambdas called the objects
  2. you didn't get the value of the entry properly
  3. you changed up the entries and output widgets.

The final code:

from tkinter import * 
root = Tk()
root.title("Simple Calculator")

out_mean = Label(root, text="Mean") 
out_min = Label(root, text="Min") 
out_max = Label(root, text="Max") #shows as text in the window

e_mean = Entry(root,width= 30, borderwidth = 5) 
e_min = Entry(root,width= 30, borderwidth = 5) 
e_max = Entry(root,width= 30, borderwidth = 5)

def click_mean():
    out_mean.configure(text=e_mean.get())
    e_mean.delete(0, END)
def click_min():
    out_min.configure(text=e_min.get())
    e_mean.delete(0, END)

def click_max():
    out_max.configure(text=e_max.get())
    e_max.delete(0, END)

button_mean = Button(root, text = "mean", padx = 40, pady = 20, command = click_mean)  
button_min = Button(root, text = "min", padx = 40, pady = 20, command = click_min) 
button_max = Button(root, text = "max", padx = 40, pady = 20, command = click_max)

e_mean.grid(row=0,column=0) 
e_min.grid(row=1,column=0) 
e_max.grid(row=2,column=0)

button_mean.grid(row=0,column=1) 
button_min.grid(row=1,column=1) 
button_max.grid(row=2,column=1)

out_mean.grid(row=0,column=2, columnspan = 2) 
out_min.grid(row=1,column=2, columnspan = 2) 
out_max.grid(row=2,column=2, columnspan = 2)

root.mainloop()

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