所以我只想制作一個簡單的表格,用戶在其中輸入平均值、最小值和最大值。然后這將顯示在 Tkinter 的 GUI 上。我想我快到了,但由于某種原因,我似乎無法編輯我創建的標簽。
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)
uj5u.com熱心網友回復:
您的代碼有幾個問題。
首先,您的按鈕命令不正確。如果您已經定義了不需要輸入的函式,則不需要使用 lambda。您的 lambda 函式也沒有呼叫正確的函式。在任何時候,您都不會呼叫任何click_mean,click_min或click_max。
您的第二個錯誤是將條目作為引數傳遞以更改文本,而不是輸入到條目中的文本。您需要呼叫e_mean.get()以獲取字串值。
您的第三個錯誤是您試圖編輯標簽的文本值,就好像它們是條目一樣。正確的叫法是<Label_name>.configure(text=e_mean.get())。
修復所有這些錯誤,您的作業代碼現在是:
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()
雖然您不會注意到錯誤(因為 tkinter 很聰明并且可以不用),但您沒有root.mainloop()在腳本末尾呼叫。雖然目前不會導致任何錯誤,但這樣做是一種很好的做法,并且在某些情況下可能會導致錯誤。您也不應該對 tkinter ( from tkinter import *) 使用通配符匯入。而是明確定義匯入import tkinter as tk或類似。
uj5u.com熱心網友回復:
你改變了一些東西:
- lambdas 稱為物件
- 您沒有正確獲得條目的值
- 您更改了條目和輸出小部件。
最終代碼:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/413724.html
標籤:
