简体   繁体   English

如何在 GUI tkinter 上显示输入

[英]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.这将显示在 Tkinter 的 GUI 上。 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.当您已经定义了不需要输入的 function 时,您不需要使用 lambda。 Your lambda functions are also not calling the correct function.您的 lambda 函数也没有调用正确的 function。 At no point do you call any of click_mean , click_min or click_max .在任何时候,您都不会调用click_meanclick_minclick_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.您需要调用e_mean.get()来获取字符串值。

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()) .正确的调用是<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.虽然您不会注意到错误(因为 tkinter 很聪明并且可以不用),但您没有在脚本末尾调用root.mainloop() 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 * ).您也不应该对 tkinter 使用通配符导入( from tkinter import * )。 Instead explicitly define the import, import tkinter as tk or similar.而是明确定义导入, import tkinter as tk或类似名称。

You changed up some of the things:你改变了一些东西:

  1. the lambdas called the objects lambdas 称为对象
  2. you didn't get the value of the entry properly您没有正确获得条目的值
  3. you changed up the entries and output widgets.您更改了条目和 output 小部件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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