我正在學習使用 Python 和 Tkinter,所以我正在使用這個簡單的計算器應用程序。我的主要問題來自函數calculator_tasks:在第一個條件下,應該刪除0,但它沒有這樣做,為什么呢?我試圖在按下按鈕時獲取文本,但它只打印“/”,這有什么問題?請參閱下面的完整代碼,這樣您也許可以幫助我。from tkinter import *import tkinter as tkclass calculator: def __init__(self, master): self.master = master master.title("Calculator") master.geometry("10x10") grid_size = 4 a=0 while a <= grid_size: master.grid_rowconfigure(a,weight=1, uniform='fred') master.grid_columnconfigure(a,weight=1, uniform='fred') a += 1 self.ini_frame = Frame(master,bg="light blue",highlightthickness=2, highlightbackground="black") self.calc_title = Label(self.ini_frame, text ="Calculator",bg="light blue") self.calculation_frame = Frame(master,bg="black") self.calculation = IntVar(master,0) self.calc_figure = Text(self.calculation_frame,fg="white",bg='black') self.calc_figure.insert(END,0) self.ini_frame.grid(columnspan=4,sticky=NSEW) self.calculation_frame.grid(row=1,columnspan=4,sticky=NSEW) self.calc_title.grid(padx=20) self.calc_figure.grid(pady=20,padx=20) master.update_idletasks() def calculator_tasks(): if self.calc_figure.get("1.0",END) == 0: self.calc_figure.delete(0,END) self.calc_figure.insert(END,i) else: self.calc_figure.insert(END,i) r = 2 c = 0
1 回答

犯罪嫌疑人X
根據你的問題:
TA貢獻2080條經驗 獲得超4個贊
這是一個簡單的例子:
根據你的問題:Retrieve text from button created in a loop
import tkinter as tk root = tk.Tk()####### Create Button ###############def get_text(text): print(text)for i in range(10): text = f'Hello I am BeginnerSQL74651 {i}' button = tk.Button(root, text=text, command=lambda button_text=text: get_text(button_text)) button.grid() root.mainloop()
添加回答
舉報
0/150
提交
取消