构建一个tkinter GUI模块,因此主程序不需要导入tkinter

时间:2017-11-18 23:43:30

标签: python python-3.x user-interface tkinter

我想创建一个GUI模块,我可以在我的主程序中导入,而不必在那里导入tkinter,只让模块处理所有内容。以下是我的想象,它可能会起作用:

  

main.py

import gui as g

def update():
    #Update the GUI with new Data from this main program

GUI = g.gui()
gui.after(1000, update)
gui.mainloop()
  

gui.py

import tkinter as tk

class viewer(tk.Frame):
    #Some variables
    def __init__(self, parent):
        tk.Frame.__init(self, parent)
        self.parent = parent
        self.initialize(400, 100)
    def initialize(self, width, height):
        #Initialize some widgets, place them on grid, etc
    def start(self):
        #Do some other stuff, make a main window, configurations, etc
        print('Started!')

编辑:"不要求意见" 我如何使这项工作?

import tkinter as tk
import gui as g

root = tk.Tk()
GUI = g.gui(root)
GUI.after(1000, update)
GUI.mainloop()

以上是我不想要的。

1 个答案:

答案 0 :(得分:1)

我使用了似乎合情合理的解决方法:

  

main.py

import gui
GUI = gui.start()
GUI.after(1000, update)
GUI.mainloop()
  

gui.py

import tkinter as tk
def start():
   root = tk.Tk()
   run = viewer(root) # <- The class provided above
   return run