为了账号安全,请及时绑定邮箱和手机立即绑定

Tkinter - 从循环中创建的按钮检索文本

Tkinter - 从循环中创建的按钮检索文本

茅侃侃 2023-10-26 16:56:53
我正在学习使用 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()



查看完整回答
反对 回复 2023-10-26
  • 1 回答
  • 0 关注
  • 58 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信