我需要幫助
我一直在嘗試用 python 制作一個計算器程式,我做得很好。
我想使用相同的代碼和 Tkinter 庫為其創建一個視窗,但它讓我很困惑。
我的計算器代碼是:
while True:
num1 = 0
num2 = 0
num3 = 0
print("Enter your calculation:")
calc_in = str(input()).split()
try:
num1 = int(calc_in[0])
op1 = calc_in[1]
num2 = int(calc_in[2])
except:
print("Error: Make sure you typed your sum correctly")
continue
try:
op2 = calc_in[3]
num3 = int(calc_in[4])
except:
pass
if op1 == " ":
answer = num1 num2
elif op1 == "-":
answer = num1 - num2
elif op1 == "*":
answer = num1 * num2
elif op1 == "/":
answer = num1 / num2
try:
if op2 == " ":
answer = answer num3
elif op2 == "-":
answer = answer - num3
elif op2 == "*":
answer = answer * num3
elif op2 == "/":
answer = answer / num3
except:
pass
print(answer)
并且代碼作業正常,但是當我嘗試使用入口小部件進行修補時,它給我帶來了一些奇怪的東西。
到目前為止,這是 Tkinter 的代碼(我還沒有 tkinter 輸出位,因為我想看看它是否真的可以作業):
import tkinter as tk
import tkinter.ttk as ttk
window = tk.Tk()
label = tk.Label(
text="Calculator",
foreground="white",
background="dodgerBlue",
width=15,
height=0,
font=("Arial", 25),
)
label.pack()
calc_input = tk.Entry(
foreground="dodgerBlue",
background="white",
width=15,
font=("Arial", 25),
)
calc_input.pack()
window.mainloop()
while True:
num1 = 0
num2 = 0
num3 = 0
convert = str(calc_input.get)
calc_in = convert.split()
print(calc_in)
break
try:
num1 = int(calc_in[0])
op1 = calc_in[1]
num2 = int(calc_in[2])
except:
print("Error: Make sure you typed your sum correctly")
continue
try:
op2 = calc_in[3]
num3 = int(calc_in[4])
except:
pass
if op1 == " ":
answer = num1 num2
elif op1 == "-":
answer = num1 - num2
elif op1 == "*":
answer = num1 * num2
elif op1 == "/":
answer = num1 / num2
try:
if op2 == " ":
answer = answer num3
elif op2 == "-":
answer = answer - num3
elif op2 == "*":
answer = answer * num3
elif op2 == "/":
answer = answer / num3
except:
pass
print(answer)
當我單擊輸入時,沒有任何反應,但是當我關閉視窗時,我得到一個奇怪的陣列:
['<bound', 'method', 'Entry.get', 'of', '<tkinter.Entry', 'object', '.!entry>>']
我不明白發生了什么,所以任何幫助將不勝感激。
uj5u.com熱心網友回復:
您的代碼有兩個主要問題。
- 第一:
calc_input.get這行代碼是錯誤的,因為你沒有呼叫需要用到這個的函式calc_input.get() - 第二:在
calc_input.get()這行代碼之后你會得到一個錯誤_tkinter.TclError: invalid command name ".!entry"因為你試圖在視窗被銷毀后從輸入框中獲取值。
格式化代碼在這里
import tkinter as tk
import tkinter.ttk as ttk
convert = 0
window = tk.Tk()
label = tk.Label(
text="Calculator",
foreground="white",
background="dodgerBlue",
width=15,
height=0,
font=("Arial", 25),
)
label.pack()
calc_input = tk.Entry(
foreground="dodgerBlue",
background="white",
width=15,
font=("Arial", 25),
)
calc_input.pack()
def on_closing():
global convert # Exccess to the global variable which is define in line 3.
convert = calc_input.get() # set the value to convert
window.destroy() # destroy the window
window.protocol('WM_DELETE_WINDOW',on_closing) # This is execute when someone try to exit or destroy the window And call the on_closing function
window.mainloop()
while True:
num1 = 0
num2 = 0
num3 = 0
calc_in = convert.split()
print(calc_in)
break
try:
num1 = int(calc_in[0])
op1 = calc_in[1]
num2 = int(calc_in[2])
except:
print("Error: Make sure you typed your sum correctly")
continue
try:
op2 = calc_in[3]
num3 = int(calc_in[4])
except:
pass
if op1 == " ":
answer = num1 num2
elif op1 == "-":
answer = num1 - num2
elif op1 == "*":
answer = num1 * num2
elif op1 == "/":
answer = num1 / num2
try:
if op2 == " ":
answer = answer num3
elif op2 == "-":
answer = answer - num3
elif op2 == "*":
answer = answer * num3
elif op2 == "/":
answer = answer / num3
except:
pass
print(answer)
我昨天解決了同樣的問題你可以看看鏈接。
uj5u.com熱心網友回復:
您必須記住,在每個 GUI 應用程式中,您應該做的所有事情都必須在您的 mainloop() 之前(或內部),因為這是 GUI 應用程式的本質,它們是一個回圈并檢查應用程式的狀態。
我看到你的代碼并注意到很多錯誤:
在 mainloop() 函式之后,您撰寫一些代碼(While 回圈)。mainloop() 之后的每個代碼都將在您關閉視窗后執行,您應該記住,小部件將在您關閉視窗后被銷毀。所以這就是為什么我應該說你必須在你的 mainloop() 函式中擁有你想要的一切。
如果您想獲取您的條目值,您可以添加一個按鈕,然后單擊按鈕可以列印條目的值。(當然你應該閱讀Button中command屬性的說明)
請記住,每個小部件都必須將根(或您在代碼中撰寫的視窗)作為第一個引數。例如標簽(視窗,“這是標簽”)。
最后:在一個while回圈中,當你寫break命令時,其余的代碼永遠不會被執行。所以所有那些嘗試除了塊,永遠不會被執行。
這是我用你的小部件樣式為你寫的一個例子:
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label = tk.Label(self,
text="Calculator",
foreground="white",
background="dodgerBlue",
width=15,
height=0,
font=("Arial", 25),
)
self.label.pack()
self.entry = tk.Entry(self,
foreground="dodgerBlue",
background="white",
width=15,
font=("Arial", 25),)
self.entry.pack()
self.button = tk.Button(self, text="Print Value",
command=self.print_entry_value)
self.button.pack()
self.mainloop()
def print_entry_value(self) -> None:
"""
This widget will print the entry value
"""
value = self.entry.get()
print(value)
if __name__ == "__main__":
window = MainWindow()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/468260.html
