代码之家  ›  专栏  ›  技术社区  ›  t.hall

如何使用触摸屏为tkinter中的函数调用选择特定的条目小部件?

  •  1
  • t.hall  · 技术社区  · 7 年前

    我正在创建一个触摸屏GUI,其中包含使用小键盘编辑的多输入小部件。我怎么知道用户选择了哪个条目小部件?我创建了一种迂回的方式,其中每个条目都编码为一个按钮,单击该按钮时,该按钮会编辑一个“选定”变量,然后键盘会使用该变量来知道要编辑哪个,但有更好的方式吗?可能使用焦点?(我有按钮小部件工作,但我想使用注释条目小部件,还删除了一些不相关的代码,因此帖子不会太长)

    class MyApp(Tk):
        def __init__(self,*args, **kwargs):
            Tk.__init__(self, *args, **kwargs)
    
            container = Frame(self)
            container.pack(side="top", fill="both", expand = True)
    
            self.shared_data = {
                'codeOne' : StringVar(),
                'codeTwo' : StringVar(), 
                'selectedCode' : StringVar(),
            }
    
        def setCode(self, value):
            #function updates selected variable
            selectedVar = self.shared_data['selectedCode']
            if selectedVar.get() == 'codeOne':
                code = self.shared_data['codeOne']
            elif selectedVar.get() == 'codeTwo':
                code = self.shared_data['codeTwo']
            else: #non selected
                print('nothing selected')
                return False
            old = code.get()
            if type(value) == int:
                code.set(old+str(value))
            else:
                code.set(old[0:len(old)-1])
            return True
    
        def setVariable(self, variable, value):
            variable.set(value)
            return True
    
    class MenuPage(Frame):
        def __init__(self, parent, controller):
            Frame.__init__(self, parent)
            self.controller = controller
    
            Label(self, text="Code 1:", font='Helvetica 15').grid(row=2, column=0, columnspan=3, sticky=E, pady=10)
            Button(self, textvariable=controller.shared_data['codeOne'], font=MENU_ENTRIES, width=7, bg='grey99', command=lambda:controller.setVariable(controller.shared_data['selectedCode'],'codeOne')).grid(row=2, column=1, pady=10)
            #Entry(self,  textvariable=controller.shared_data['codeOne'], font='Helvetica 15').grid(row=2, column=3, columnspan=1, pady=10, sticky=W)
    
            Label(self, text="Code 2:", font='Helvetica 15').grid(row=3, column=0, columnspan=3, sticky=E, pady=10)
            Button(self, textvariable=controller.shared_data['codeTwo'], font=MENU_ENTRIES, width=7, bg='grey99', command=lambda:controller.setVariable(controller.shared_data['selectedCode'],'codeTwo')).grid(row=2, column=1, pady=10)
            #Entry(self, textvariable=controller.shared_data['codeTwo'], font='Helvetica 15').grid(row=3, column=3, columnspan=3, pady=10, sticky=W)
    
            Button(self, text="1", width=3, command=lambda:controller.setCode(1)).grid(row=1, column=3, rowspan=2, padx=(20,5), pady=10)
            Button(self, text="2", width=3, command=lambda:controller.setCode(2)).grid(row=1, column=4, rowspan=2, padx=5, pady=10)
            Button(self, text="3", width=3, command=lambda:controller.setCode(3)).grid(row=1, column=5, rowspan=2, padx=5, pady=10)
            Button(self, text="4", width=3, command=lambda:controller.setCode(4)).grid(row=3, column=3, rowspan=2, padx=(20,5), pady=10)
            Button(self, text="5", width=3, command=lambda:controller.setCode(5)).grid(row=3, column=4, rowspan=2, padx=5, pady=10)
            Button(self, text="6", width=3, command=lambda:controller.setCode(6)).grid(row=3, column=5, rowspan=2, padx=5, pady=10)
            Button(self, text="7", width=3, command=lambda:controller.setCode(7)).grid(row=5, column=3, rowspan=2, padx=(20,5), pady=10)
            Button(self, text="8", width=3, command=lambda:controller.setCode(8)).grid(row=5, column=4, rowspan=2, padx=5, pady=10)
            Button(self, text="9", width=3, command=lambda:controller.setCode(9)).grid(row=5, column=5, rowspan=2, padx=5, pady=10)
            Button(self, text="DELETE", width=7, command=lambda:controller.setCode('delete')).grid(row=7, column=3, rowspan=2, columnspan=2, padx=(20,5), pady=10)
            Button(self, text="0", width=3, command=lambda:controller.setCode(0)).grid(row=7, column=5, rowspan=2, padx=5, pady=10)
    
     app = MyApp()
     app.mainloop()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Bryan Oakley    7 年前

    删除按钮,放回条目小部件,然后更改 setCode 为此:

    def setCode(self, value):
        # get the widget with the focus
        widget = self.focus_get()
    
        # insert the value
        widget.insert("insert", value)
    

    grid pack place

    entry = Entry(self, textvariable=controller.shared_data['codeOne'], width=7, bg='grey99')
    entry.grid(row=2, column=1, pady=10)
    entry.focus_set()