![](/img/trans.png)
[英]How do i use an 'if' on a checkbutton in tkinter. (python 3.8)
[英]How do I change from the Checkbutton on tkinter
所以我對 Tkinter 很陌生,真的不知道如何使用它。 我查找了一些類似的代碼並找到了如何獲取我用來注冊值的文本框 (e1)(e6) 的解決方案,但缺點是我需要使用第 52 行中的復選框。有誰知道如何我可以停止使用復選框,而不必單擊復選框而只需輸入值嗎?
#importerar tkinter
from tkinter import *
from tkinter import ttk
#All calculations are made here, which are then displayed and registered before you save the receipt.
def show():
totalt = 0
if (japp.get()):
pris = int(e1.get())
kvk = int(e6.get())
totalt = int(pris * kvk)
tempList = [[e1.get(), e6.get(), totalt]]
tempList.sort(key=lambda e: e[1], reverse=True)
for i, (pris, kvk, totalt) in enumerate(tempList, start=1):
listBox.insert("", "end", values=(pris, kvk, totalt))
sum1 = 0.0
for lmao in listBox.get_children():
sum1 += float(listBox.item(lmao, 'values')[2])
totaltText.set(sum1)
#Here the receipt is saved, geom that it goes through each number and then enters it in the txt file.
def spara():
for lmao in listBox.get_children():
pris = str(listBox.item(lmao, 'values')[0])
kvk = str(listBox.item(lmao, 'values')[1])
totalt = str(listBox.item(lmao, 'values')[2])
txt = open("Kvitto.txt", "w")
txt.write("kvantitet: " + kvk + ". Pris per vara: " + pris + ". total kostnad: " + totalt + ". ")
txt.close
hej = Tk()
hej.title("Kvitto system")
hej.geometry("650x650")
global e1
global totaltText
global balText
totaltText = StringVar()
balText = IntVar()
#all text som visas
Label(hej, text="Kvitto system", font="arial 22 bold" ,bg="white").place(x=5, y=10)
Label(hej, text="Antal", font="arial 13").place(x=10, y=80)
japp = IntVar()
Checkbutton(hej, text="Kostnad per styck", variable=japp).place(x=10, y=50)
#skriv rutorna, där man svarar på kvantitet(e6) och kostnad(e1)
e1 = Entry(hej)
e1.place(x=200, y=80)
e6 = Entry(hej)
e6.place(x=200, y=50)
totalt = Label(hej, text="", font="arial 22 bold", textvariable=totaltText)
totalt.place(x=650, y=10)
#Alla knappar som används i programmet
Button(hej, text="Lägg till varor", command=show, height=3, width=13).place(x=10, y=220)
cols = ('Pris', 'Kvantitet', 'total kostnad')
listBox = ttk.Treeview(hej, columns=cols, show='headings')
Button(hej, text="Spara Kvitto", command=spara, height=3, width=13).place(x=150, y=220)
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=2)
listBox.place(x=10, y=300)
hej.mainloop()
很簡單,只需刪除此復選框和與之關聯的所有內容,例如檢查此復選框等。
對於初學者,在刪除這些行后調整列表:
if (japp.get()):
...
japp = IntVar()
Checkbutton(hej, text="Kostnad per styck", variable=japp).place(x=10, y=50)
關閉頂部:
這很奇怪,如果您創建了該代碼,您應該能夠修改它。 你應該做的第一件事是在類中組織你的代碼,以便於管理和調試......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.