Python 从单独的模块导入tkinter按钮
我们有一个功能强大的程序,它使用Tkinter作为GUI。一切正常,但不同的代码分支现在使用不同的硬件,实际需要不同的按钮。因此,我们希望有代表按钮的主要GUI导入模块,具体取决于所使用的硬件 我删掉了下面的一些代码,我想将makemenu函数删除到一个单独的模块中,因此当它在应用程序init self.makemenumaster中调用时,我想将其作为一个单独模块的引用。我试过这样做,但遇到了麻烦。这可能吗 我对父结构有点困惑,需要向我的按钮模块传递什么,等等。?我知道这是一个结构拙劣的问题,但如果有人能够建议这是否可行,并将我的想法放在正确的轨道上,那就太好了。例如,如果有人可以演示如何修改代码,使按钮在单独的模块中定义,我就可以在我的模块中找到同样的方法Python 从单独的模块导入tkinter按钮,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我们有一个功能强大的程序,它使用Tkinter作为GUI。一切正常,但不同的代码分支现在使用不同的硬件,实际需要不同的按钮。因此,我们希望有代表按钮的主要GUI导入模块,具体取决于所使用的硬件 我删掉了下面的一些代码,我想将makemenu函数删除到一个单独的模块中,因此当它在应用程序init self.makemenumaster中调用时,我想将其作为一个单独模块的引用。我试过这样做,但遇到了麻烦。这可能吗 我对父结构有点困惑,需要向我的按钮模块传递什么,等等。?我知道这是一个结构拙劣的问题,
# 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()
如果您想将makemenu移动到一个单独的模块,那应该非常简单。但是,您需要更改一些内容
由于makemenu不再具有对self的引用或具有不同的引用,如果将其作为一个单独的类实现,则需要将诸如command=lambda:self.geterror'alt'之类的调用替换为command=lambda:master.geterror'alt'
我建议的另一件事是删除将菜单添加到根目录的调用。我相信模块不应该有这样的副作用-函数应该制作一个菜单并返回它,让调用者决定如何使用它,即:
self.menubar=makemenu(master)
master.configure(menu=self.menubar)
粗略地说,这是MVC模型/视图/控制器体系结构模式的一种变体,其中应用程序实例是您的控制器,也是视图的一部分,除非您为所有UI代码制作模块。菜单是视图的一部分,将UI功能转发给控制器执行
然后,您的应用程序看起来如下所示:
from makemenu import makemenu
class Application(...):
def __init__(...):
...
self.menubar = makemenu(master)
master.config(menu=self.menubar)
...