![](/img/trans.png)
[英]problem with font size in following python tkinter code whenever i run my program font size do not increase
[英]What is the problem with my code run on Tkinter?
我是Python Tkinter的新手,我想創建一個在其上運行的程序。 但是,我的代碼無法正常工作。
from tkinter import *
def conv1(self):
gbp0 = 174000000
galleons0 = 34000872
sickles0 = 14
knuts0 = 7
galleons1 = float(galleons0 + sickles0 / 17 + knuts0 / 29 / 17)
fracture = float(gbp0 / galleons1)
convert1 = Toplevel(root)
convert1.title("Pounds Sterling (GBP) to Galleons, Sickles and Knuts Converter")
label1_1 = Label(convert1, text="Type the amount of money in GBP that you would like to convert to Galleons, Sickles and Knuts and press Enter.")
label1_2 = Label(convert1, text="1 Galleon = 5.12 GBP")
label1_3 = Label(convert1, text='GBP:')
label1_1.pack()
label1_2.pack()
label1_3.pack()
usergbpvar = DoubleVar()
usergbp = Entry(convert1, textvariable=usergbpvar)
usergbp.pack()
a = float(usergbpvar.get() / fracture)
galleons = int(a // 1)
a = (a % 1) * 17
sickles = int(a // 1)
a = (a % 1) * 29
if (a % 1) == 0.5:
knuts = int(round(a, 0))
knuts += 1
else:
knuts = int(round(a, 0))
galleons, sickles, knuts = str(galleons), str(sickles), str(knuts)
label1_4 = Label(convert1, text=galleons)
label1_5 = Label(convert1, text=sickles)
label1_6 = Label(convert1, text=knuts)
label1_4.pack()
label1_5.pack()
label1_6.pack()
convert1.mainloop()
root = Tk()
btn1 = Button(root, text='GBP to Galleons, Sickles and Knuts', bg='#555', fg='#ccc', font='16')
btn1.pack()
btn1.bind('<Button-1>', conv1)
root.mainloop()
它應該從輸入的數字中計算出三個數字,並將其顯示在屏幕上。 但是,當我運行該程序時,按下按鈕后,我看到所有數字都已經存在並且它們都是0。輸入我的數字后,什么都沒有改變。
您能告訴我代碼中的問題在哪里嗎?
問題/問題1:
當我運行該程序時,按下按鈕后,我看到所有數字都已經存在並且它們都是0。
當您調用label1_4=Label(convert1, text=galleons) label1_4.pack()
這會告訴label1_4=Label(convert1, text=galleons) label1_4.pack()
立即顯示具有給定值的標簽,例如,label1_4的加侖數為0(與其他標簽相同)。 這不是問題,可以預期,因為開始時輸入框的值為0。
問題/問題2:
輸入號碼后,什么都沒有改變。
您實際上並沒有告訴程序曾經更新標簽的值。 就像TornaxO7所說的那樣,您需要綁定Enter(返回)鍵來調用函數usergbp.bind("<Return>", calculation_function_here)
我已經編輯了您的代碼以提供一種面向對象的方法。 我建議隨着您的進步而探索這種方法,並且可能需要多個窗口。 構造tkinter應用程序的最佳方法?
from tkinter import *
class gui_window:
def __init__(self, master):
# setup gui
self.master = master
self.master.wait_visibility() # attempt to fix traceback error, see Problem/question 3 below
self.master.grab_set() # stops button1 creating another gui_window instance
self.master.title('Pounds Sterling (GBP) to Galleons, Sickles and Knuts Converter')
self.label1_1=Label(master, text="Type the amount of money in GBP that you would like to convert to Galleons, Sickles and Knuts and press Enter.")
self.label1_1.pack()
self.label1_2=Label(master, text="1 Galleon = 5.12 GBP")
self.label1_2.pack()
self.label1_3=Label(master, text='GBP:')
self.label1_3.pack()
self.usergbpvar=DoubleVar()
self.usergbp=Entry(master, textvariable=self.usergbpvar)
self.usergbp.bind("<Return>", self.calculate) # when user presses enter call the conversion function
self.usergbp.pack()
label1_4_1 = Label(self.master, text = 'Galleons:').pack(anchor = 'w')
self.label1_4=Label(self.master, text='0', anchor = 'e')
self.label1_4.pack()
label1_5_1 = Label(self.master, text = 'Sickles:').pack(anchor = 'w')
self.label1_5=Label(self.master, text='0', anchor = 'e')
self.label1_5.pack()
label1_6_1 = Label(self.master, text = 'Knuts:').pack(anchor = 'w')
self.label1_6=Label(self.master, text='0')
self.label1_6.pack()
self.gbp0=174000000
self.galleons0=34000872
self.sickles0=14
self.knuts0=7
self.galleons1=float(self.galleons0+self.sickles0/17+self.knuts0/29/17)
self.fracture=float(self.gbp0/self.galleons1)
def calculate(self, event):
# do calculation
a=float(self.usergbpvar.get()/self.fracture)
galleons=int(a//1)
a=a%1
a=a*17
sickles=int(a//1)
a=a%1
a=a*29
if a%1==0.5:
knuts=int(round(a, 0))
knuts=knuts+1
else:
knuts=int(round(a, 0))
galleons=str(galleons)
sickles=str(sickles)
knuts=str(knuts)
# update the labels to reflect the calculation
self.label1_4.config(text=galleons)
self.label1_5.config(text=sickles)
self.label1_6.config(text=knuts)
def create_gui(self):
# create a gui_window Toplevel instance
convert1=Toplevel()
gui_window(convert1)
root=Tk()
btn1=Button(root, text='GBP to Galleons, Sickles and Knuts', bg='#555', fg='#ccc', font='16')
btn1.pack()
btn1.bind('<Button-1>', create_gui) # call function to make next window
root.mainloop()
問題/來自評論的問題3:我相信錯誤: tkinter.TclError: grab failed: window not viewable
取決於您的操作系統。 我無法在Mac OS上重現該錯誤,但添加self.master.wait_visibility()
(添加到我的代碼中)可能會解決此問題: python tkinter treeview不允許使用直接綁定(如on_rightclick)的模式窗口
我猜您忘了綁定Return
鍵。
您應該在您的方法中添加convert1.bind("<Return>", *your function*)
。
“您的功能”是更改數字的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.