从单独的模块导入tkinter按钮

时间:2014-12-10 17:47:27

标签: python python-2.7 tkinter

我们有一个功能强大的程序,它使用Tkinter作为GUI。一切正常,但代码的不同分支现在使用不同的硬件,实际上需要不同的按钮。因此,我们希望主要的GUI导入模块代表按钮,具体取决于所使用的硬件。

我已经删除了下面的一些代码,我有兴趣将makemenu()函数移除到单独的模块,因此在应用程序__init__中调用它时( self.makemenu(master))我想提到一个单独的模块。我已经尝试过这样做并且遇到了麻烦。这甚至可能吗?

我对父结构有点困惑,需要传递给我的按钮模块等等?我知道这是一个构造不良的问题,但是如果有人能够建议这是否可行并让我走上正确的轨道那将是伟大的。例如,如果有人可以展示如何修改this代码以在单独的模块中定义按钮,我可以弄清楚如何在我的模块中执行相同操作。

# Import necessary libraries
import sys

import os

import Tkinter as tk



class Application(tk.Frame):

    ##################################################################
    ## Final functions are designed to initialize the GUI and
    ## connect various mouse movements to useful functions.
    ##################################################################

    def definevars(self):
        '''Original definition of all of the key variables that
        we need to keep track of while running the GUI

        '''    
        self.disable = True
        self.savimgstatus = 'off'
        self.mode = 'Standby'
        self.status = 'Not Ready'




    def makemenu(self,master):
        ''' Function to create the main menu bar across
        the top of the GUI.

        '''

        self.menubar = tk.Menu(master)


        ## Motor Submenu
        motormenu = tk.Menu(self.menubar,tearoff=1)
        motormenu.add_command(label='ALT',state='disabled')
        motormenu.add_command(label='error check',
                command=lambda: self.geterror('alt'))
        motormenu.add_separator()
        motormenu.add_command(label='AZ',state='disabled')
        motormenu.add_command(label='error check',
                command=lambda: self.geterror('az'))
        self.menubar.add_cascade(label='Tracker Motors',menu=motormenu)


        ## Set the big menu as the main menu bar.
        master.config(menu=self.menubar)





    def __init__(self,tcpconn,DOME,TRACKERSTAGE, master=None):
        '''Main function to initialize the GUI.  Will scale
        the size of the GUI to fit any size screen... to a
        point.  It will not allow it to be smaller than 
        600x800.

        '''


        self.buf = 1024

        ## Check resolution of screen.  Make GUI 2/3rds of size 
        ## unless that means under 600x800.
        fh = round(master.winfo_screenheight()*2./3.)
        fw = round(master.winfo_screenwidth()*2./3.)
        if fh < 600: fh = 600
        if fw < 800: fw = 800
        print 'GUI resolution set to {0} x {1}'.format(fw,fh)
        self.fw = fw
        self.fh = fh
        self.imwidth = int(0.45*self.fw)
        self.imheight = int(0.45*self.fh)
        self.imcentx = self.imwidth/2
        self.imcenty = self.imheight/2this

        ## Initialize Frame
        tk.Frame.__init__(self, master, height=fh,width=fw)
        self.grid()
        self.grid_propagate(0)

        ## Initialize Various variables.
        self.definevars()

        ## Create buttons, etc.
        self.createWidgets()
        self.makemenu(master)
        self.disableall()

        ## Main Loop function
        self.checkoutput()

###################################################################


# Initialize GUI window.

root = tk.Tk()

root.title('Hardware') # window title

app = Application(master=root)

app.mainloop() # go into the main program loop

sys.exit()

1 个答案:

答案 0 :(得分:0)

如果您想将makemenu移动到单独的模块,那应该非常简单。但是,您需要更改一些内容。

由于makemenu不再引用self(或者具有不同的引用,如果您将其作为单独的类实现),则需要将command=lambda: self.geterror('alt'))之类的调用替换为command=lambda: master.geterror('alt'))

我建议的另一件事是删除调用以将菜单添加到根目录。我认为模块不应该有这样的副作用 - 函数应该创建一个菜单并返回它,让调用者决定如何使用它,即:

self.menubar=makemenu(master)
master.configure(menu=self.menubar)

粗略地说,这是MVC(模型/视图/控制器)架构模式的变体,其中Application实例是您的控制器(除非您创建所有UI代码的模块,否则也是视图的一部分) 。该菜单是视图的一部分,并将UI功能转发给控制器以供执行。

您的应用程序看起来像这样:

from makemenu import makemenu

class Application(...):
    def __init__(...):
        ...
        self.menubar = makemenu(master)
        master.config(menu=self.menubar)
        ...
相关问题