简体   繁体   English

跨多个文件/屏幕绑定快捷方式Python3.3 Tkinter

[英]Binding a shortcut accross multiple files/screen Python3.3 Tkinter

I am running a into a bit of a problem by over modulization in my current app. 我在当前应用中过度调制会给我带来一些麻烦。 I trying to bind a shortcut to the program but for the life of my I can't figure out where to bind it. 我试图将快捷方式绑定到程序,但是在我的生命中,我一直无法弄清楚将其绑定到何处。 I would prefer it work at all "screens" of the application and I will handle the saving and switching safely later. 我希望它可以在应用程序的所有“屏幕”上正常工作,稍后我将安全地保存和切换。 Any help would be appreciated. 任何帮助,将不胜感激。 I also have a global file that contains all the global variables for all the files. 我还有一个全局文件,其中包含所有文件的所有全局变量。 So in short where would I place the bind and to what exactly. 简而言之,我将绑定放置在何处以及确切放置在何处。 (Sorry for not including a small runnable sample but I can make one if need be.) (很抱歉没有包含一个小的可运行样本,但如有需要,我可以提供一个。)

class Application(tk.Frame):
    def __init__(self, w, h, master=None):

        tk.Frame.__init__(self, master)
        tk_screens = scr.GUIScreens(self)
        self.grid()

        # master.bind("Left",print("Something"))

        self.window_width = w
        self.window_height = h
        globals.window_width = w
        globals.window_height = h

        # print(window_width,window_height)
        # Application.bind('<<Return>>',Application.test(self,"enter"))
        print(Application)

        tk_screens.home_screen(True)
        tk_screens.query_screen(False)
        tk_screens.res_screen(False)
        tk_screens.settings_screen(False)
        tk_screens.customer_screen(False)


        # print(master.winfo_width())
        # self.update()

    def init_widget_state(self, dict, state):

        if not state:
            for i in dict:
                dict[i].grid_remove()
        else:
            for i in dict:
                dict[i].grid()

    def swap(self, old, new):
        for i in old:
            old[i].grid_remove()
        # self.query_screen()
        for i2 in new:
            new[i2].grid()

    def test(self,event):
        print("Clicked a button:"+event)


if __name__ == "__main__":
    main = tk.Tk()
    main.wm_title("Title")
    main.geometry("1800x900")
    # main.state("zoomed")
    main.bind('<<Return>>',Application.test(self=main,event="enter"))

    main.update()
    globals.init()
    app = Application(master=main, w=main.winfo_width(), h=main.winfo_height())
    app.mainloop()

This is the seperate file that contains all the screens. 这是包含所有屏幕的单独文件。

class GUIScreens():
    def __init__(self, frame):
        self.frame = frame
        self.frame.bind('<Return>',lambda :main.Application.test(self,"enter"))


    def home_screen(self, state):
        self.frame.bind('<<Return>>',main.Application.test(self,"enter"))

        """This where I declare all the widgets for this "screen""""

        self.frame.bind('<F1>',print("Test"))

        main.Application.init_widget_state(self, globals.home_widgets, state)

PS. PS。 OS is Ubuntu 操作系统是Ubuntu

Use bind_all on your Tk instance to bind every widget to the return key: Tk实例上使用bind_all将每个小部件绑定到返回键:

main.bind_all("<Return>", event)

Additionally, you should check how you're using functions as parameters. 此外,您应该检查如何将函数用作参数。 Sometimes, you're correctly using lambda , but sometimes, you're not, just using the return value of the function, which defaults to None . 有时,您正确使用了lambda ,但有时却没有,仅使用函数的返回值(默认为None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM