這個想法是通過可迭代在標簽中列印一些輸出變數。它只創建了一個標簽,并不是預期的結果。
我嘗試通過在Cost()函式內部創建標簽來顯示結果,就像print在終端中所做的那樣,假設每次呼叫函式時都會創建標簽。
import numpy as np
from tkinter import *
# configure workspace
root = Tk()
root.title("Print cost")
root.geometry('400x400')
root.configure(bg="#202529")
sizes = ['10x15', '13x18', '15x20', '20x25', '20x30', '30x40']
# depending on the volume of prints the price change, last one is a reference price.
T_10x15_cost = [1000, 700, 600, 600]
T_13x18_cost = [120, 100, 80, 80]
T_15x20_cost = [300, 250, 100, 90]
T_20x25_cost = [1000, 800, 250, 250]
T_20x30_cost = [1200, 1000, 300, 250]
T_30x40_cost = [1500, 1200, 400, 600]
cT = (T_10x15_cost, T_13x18_cost, T_15x20_cost, T_20x25_cost, T_20x30_cost, T_30x40_cost)
# Order per size entry
left_frame = Frame (root, bg="#32383D", bd=2, padx=15, pady=5)
left_frame.place(x=10, y=10)
Label(left_frame, text='10x15').grid(row=1, column=0,padx=5, pady=5)
Label(left_frame, text='13x18').grid(row=2, column=0,padx=5, pady=5)
Label(left_frame, text='15x20').grid(row=3, column=0,padx=5, pady=5)
Label(left_frame, text='20x25').grid(row=4, column=0,padx=5, pady=5)
Label(left_frame, text='20x30').grid(row=5, column=0,padx=5, pady=5)
Label(left_frame, text='30x40').grid(row=6, column=0,padx=5, pady=5)
T_10x15 = IntVar(left_frame, value=0)
T_10x15_box = Entry(left_frame, textvariable=T_10x15, width=5)
T_13x18 = IntVar(left_frame, value=0)
T_13x18_box = Entry(left_frame, textvariable=T_13x18, width=5)
T_15x20 = IntVar(left_frame, value=0)
T_15x20_box = Entry(left_frame, textvariable=T_15x20, width=5)
T_20x25 = IntVar(left_frame, value=0)
T_20x25_box = Entry(left_frame, textvariable=T_20x25, width=5)
T_20x30 = IntVar(left_frame, value=0)
T_20x30_box = Entry(left_frame, textvariable=T_20x30, width=5)
T_30x40 = IntVar(left_frame, value=0)
T_30x40_box = Entry(left_frame, textvariable=T_30x40, width=5)
T_10x15_box.grid(row=1, column=1)
T_13x18_box.grid(row=2, column=1)
T_15x20_box.grid(row=3, column=1)
T_20x25_box.grid(row=4, column=1)
T_20x30_box.grid(row=5, column=1)
T_30x40_box.grid(row=6, column=1)
right_frame = Frame (root, bg="#32383D", bd=2, padx=15, pady=5)
right_frame.place(x=200, y=50)
# cost base on volume for each size
def runCost():
t_10x15 = int(T_10x15_box.get())
t_13x18 = int(T_13x18_box.get())
t_15x20 = int(T_15x20_box.get())
t_20x25 = int(T_20x25_box.get())
t_20x30 = int(T_20x30_box.get())
t_30x40 = int(T_30x40_box.get())
order = [t_10x15, t_13x18, t_15x20, t_20x25, t_20x30, t_30x40]
def cost():
prints = order[i]
if prints <= 50:
pricePerVolume = 0
elif 51 <= prints < 101:
pricePerVolume = 1
else:
pricePerVolume = 2
cost_ip = prints * price[pricePerVolume]
#original script without GUI. Leaving this print to check funtionallity
print(prints, ' --- ', size, 'at store = $', cost_ip, '/// REFprice = $' ,prints * price[3])
#LABEL CREATION ATTEMP #####
# 1 option
# costoIP_label = Label(root, textvariable=cost_ip)
# 2 option
costIP_label = Label(left_frame, text=' cost: ' str(cost_ip))
#placement for any of the option
costIP_label.grid(row=1, column=3,padx=5, pady=5)
for i in range(len(order)):
order[i]
size = sizes[i]
price = np.array(cT[i])
cost()
def clearButton():
T_10x15_box.delete(0, END)
T_13x18_box.delete(0, END)
T_15x20_box.delete(0, END)
T_20x25_box.delete(0, END)
T_20x30_box.delete(0, END)
T_30x40_box.delete(0, END)
def close():
root.destroy()
bottomFrame = Frame(root, bg="#4C555C", bd=2, padx=5, pady=5)
bottomFrame.place(x=10, y=200)
runCostButton = Button(bottomFrame, text='run', command=runCost)
runCostButton.grid(row=8,column=0, padx=10, pady=10)
clearButton = Button(bottomFrame, text='clear', command=clearButton)
clearButton.grid(row=9,column=0, padx=10, pady=10)
CloseButton = Button(bottomFrame, text="exit", command=close)
CloseButton.grid(row=10,column=0, padx=10, pady=10)
root.mainloop()
uj5u.com熱心網友回復:
不要在 cost() 函式中創建 costIP_label,先在 Entry 欄位之后創建它,并使用 textvariable 僅更改“text”欄位中的值。如果你想讓它在開頭不可見,那么用
grid_forget()命令隱藏它或者在里面放一個空字串“”。您從創建大小串列開始很好,但如果您不打算在程式中更改它,那么元組當然更好。
所有其他物件都依賴于它,如果您基于它創建新物件,那么將來如果它發生變化,您將無需進入程式并更改各種變數,如 T_10x15_cost、T_10x15、T_10x15_box。
假設 cT 是一個串列串列,可以轉換為字典,其中鍵來自尺寸,值來自價格串列。cT = {尺寸[0] : [1000, 700, 600, 600] ...
最好將元素放在一個回圈中,而不是重復幾乎相同的事情:
for i in range(len(sizes)):
Label(left_frame, text=sizes[i]).grid(row=i 1, column=0, padx=5, pady=5)
如果我的筆記能以某種方式幫助你,我會很高興。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491111.html
