账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python如何获取tkinter组件的名称?比如按下多个Button中的一个,返回这个被按下的Button的text?
    39
    0
    import tkinter as tk
    
    window = tk.Tk()
    window.title('计算器')
    window.geometry('360x435')
    window.attributes("-alpha", 0.95)
    window.resizable(0, 0)#窗口大小固定,不能缩放
    
    var = tk.StringVar()
    
    
    frm_1 = tk.Frame(window, width=360, height=115, bg='CadetBlue').place(x=0, y=0, anchor=tk.NW)#数字显示区
    frm_2 = tk.Frame(window, width=270, height=240, bg='DarkSeaGreen').place(x=0, y=115, anchor=tk.NW)#数字键盘
    frm_3 = tk.Frame(window, width=90, height=320, bg='Beige').place(x=270, y=115, anchor=tk.NW)#加减乘除
    frm_4 = tk.Frame(window, width=270, height=80, bg='CadetBlue').place(x=0, y=355, anchor=tk.NW)#不可循环输出的
    
    var_result = tk.StringVar()
    var_dian=tk.StringVar()
    var_dian.set('.')
    
    
    def name(u):
        u='var'+str(u)
        return u
    
    #数字显示区
    I = tk.Entry(frm_1,textvariable=var_result,font=('Arial',40),bg='grey')
    I.place(x=0,y=0,width=360,height=115)
    #获取数字显示区的文字
    
    #函数部分
    
    #计算结果的
    def show_result():
        result = I.get()
        print(eval(result))
    
    def delete():
        f.delete(1.0, tk.END)
    
    def get():
        pass
    
    
    num=1
    #数字键盘,可循环显示的
    for j in range(3):
        for i in range(3):
    
            m=0+(i)*90
            n=115+(j)*80
            b=tk.Button(frm_2,text=num,bg='WhiteSmoke',font=('Arial','20'),command=get)
            b.place(x=m,y=n,width=90,height=80)
            num=num+1
    #数字键盘,不可循环显示的
    v=0
    
    a=tk.Button(frm_4,textvariable=var_dian,bg='Gainsboro',font=('20')).place(x=0,y=355,width=90,height=80)
    b=tk.Button(frm_4,text='=',bg='Gainsboro',font=('20'),command=show_result).place(x=180,y=355,width=90,height=80)
    f=tk.Button(frm_4,text='0',bg='WhiteSmoke',font=('Arial','20')).place(x=90,y=355,width=90,height=80)
    
    
    
    #加减乘除
    q=0
    for p in ['+','-','*','/']:
        e=tk.Button(frm_3,text=p,bg='Gainsboro',font=('20')).place(x=270,y=115+q*80,width=90,height=80)
        q=q+1
    
    window.mainloop()
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 你是我预订的人i 普通会员 1楼

      在Tkinter中,你可以使用id()方法来获取组件的名称。例如,如果你想获取按下某个Button时显示的文本,你可以这样做:

      ```python import tkinter as tk

      root = tk.Tk()

      button1 = tk.Button(root, text="Button 1") button2 = tk.Button(root, text="Button 2") button3 = tk.Button(root, text="Button 3")

      button1.bind("", function=lambda x: button1.config(text=x.text)) button2.bind("", function=lambda x: button2.config(text=x.text)) button3.bind("", function=lambda x: button3.config(text=x.text))

      root.mainloop() ```

      在这个例子中,我们创建了三个Button,然后绑定了<Button-1>事件处理程序。当用户按下按钮时,config()方法会被调用,该方法会改变按钮的文本。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部