![](/img/trans.png)
[英]I have an error in python with tkinter and need help(school project)
[英]Python 3 Need help calculating all 3 functions in tKinter
如何創建一個 function,它采用 3 個變量並在按下“計算”按鈕時顯示最高值????? 以下是我當前的代碼。 我正在為用戶創建一個 GUI 來輸入 3 個不同的讀數。 如果輸入為負數或字符串,我的程序將返回無效條目,如果所有 3 個不同的讀數均正確,則單擊計算按鈕后,最高值將顯示在答案中。 answer = Label(frame, text='') answer.grid(列=1, 行=6)
用戶不需要存儲數據只顯示最高條目????? 謝謝
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
root = Tk()
root.title('Reading Total')
root.geometry('700x200')
############################# Frames ################################
frame = ttk.Frame(root, padding='3 3 12 12')
frame.grid(column=0, row=0, sticky=(N, W, E, S))
frame.columnconfigure(0, weight=1)
frame.columnconfigure(1, weight=1)
frame.columnconfigure(2, weight=1)
frame.rowconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
frame.rowconfigure(2, weight=1)
frame.rowconfigure(3, weight=1)
frame.rowconfigure(4, weight=1)
############################# Variables ################################
Item1 = StringVar()
Item2 = StringVar()
Item3 = StringVar()
## Do I need to create a Variable to store the values???
############################# Entry ################################
o_entry=ttk.Entry(frame, width=7, textvariable=Item1)
o_entry.grid(column=1, row=1, sticky=(W, E))
sul_entry=ttk.Entry(frame, width=7, textvariable=Item2)
sul_entry.grid(column=1, row=2, sticky=(W, E))
particles_entry=ttk.Entry(frame, width=7, textvariable=Item3)
particles_entry.grid(column=1, row=3, sticky=(W, E))
############################# Labels ################################
o_label = ttk.Label(frame, text='Item1:')
o_label.grid(column=0, row=1, sticky = E)
s_label = ttk.Label(frame, text='Item2:')
s_label.grid(column=0, row=2, sticky=E)
p_label = ttk.Label(frame, text='Item3:')
p_label.grid(column=0, row=3, sticky=E)
############################# Function ################################
def number_1():
try:
O = float(Item1.get())
T = (50 * O)/5
Ta = ("%0.2f" % (T))
answer["text"]= 'Reading:', str(Ta)
assert O > 0
except AssertionError:
answer.config(text="Invalid Entry")
except ValueError:
answer.config(text="Invalid Entry")
def number_2():
try:
S = float(Item2.get())
T1 = (50 * S)/20
Tb = ("%0.2f" % (T1))
answer["text"]= 'Reading:', str(Tb)
assert S > 0
except AssertionError:
answer.config(text="Invalid Entry")
except ValueError:
answer.config(text="Invalid Entry")
def number_3():
try:
P = float(Item3.get())
T2 = (60 * P)/20
Tc = ("%0.2f" % (T2))
answer["text"]= 'Reading:', str(Tc)
assert P > 0
except AssertionError:
answer.config(text="Invalid Entry")
except ValueError:
answer.config(text="Invalid Entry")
############################# Buttons ################################
g_button = ttk.Button(frame, text='Calculate', **command =) # Need to create a function that calls all 3 functions**
g_button.grid(column=1, row=4, sticky=N)
############################# End ################################
answer = Label(frame, text='')
answer.grid(column=1, row=6)
root.columnconfigure(0, weight = 1)
root.rowconfigure(0, weight = 1)
root.mainloop()
只需創建一個 function 來調用其他三個函數:
def display_items():
number_1()
number_2()
number_3()
並用按鈕調用它:
g_button = ttk.Button(frame, text='Calculate', command=display_items)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.