【问题标题】:Importing tkinter button from separate module从单独的模块导入 tkinter 按钮
【发布时间】:2014-12-10 17:47:27
【问题描述】:

我们有一个正常运行的程序,它使用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()

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    如果你想将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)
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多