2016-07-16 45 views
1

我在我的程序中創建了2個窗口,我使用了兩個類,因爲代碼很複雜,我將它在2個不同的python文件中分開。之後,我進口的第二個窗口中的文件,我怎麼能保證它不具有這種錯誤,顯示在這張照片enter image description here導入一個python文件,當主窗口按鈕點擊時創建一個窗口

原來的結果應該是這樣的點擊新窗口按鈕後,打開: enter image description here

編碼對於主窗口:

from tkinter import * 
import classGUIProgram 
class Window(Tk): 
    def __init__(self, parent): 
     Tk.__init__(self, parent) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     self.geometry("600x400+30+30") 
     self.wButton = Button(self, text='newWindow', command =  self.OnButtonClick) 
     self.wButton.pack() 

    def OnButtonClick(classGUIProgram): 
     classGUIProgram.top = Toplevel() 
     master = Tk() 
     b = classGUIProgram.HappyButton(master) 
     master.mainloop() 
if __name__ == "__main__": 
    window = Window(None) 

    window.title("title") 

    window.mainloop() 

編碼第二個窗口:

from tkinter import * 
class HappyButton: 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 
     self.printButton = Button(frame, text="Print message", command=self.printMessage) 
     self.printButton.pack(side=LEFT) 

     self.quitButton = Button(frame, text="Quit", command= quit) 
     self.quitButton.pack(side=LEFT) 

     self.downloadHistoryCB=Checkbutton(frame, text="Download History") 
     self.downloadHistoryCB.pack(side=LEFT) 

    def printMessage(self): 
     print("Wow this actually worked!") 

master = Tk() 
b = HappyButton(master) 
master.mainloop() 

回答

5

您正在創建額外的Tk窗口。這是一個使用小部件和其他文件的示例Toplevel

mainWindow.py

import tkinter as tk 
import secondWindow as sW 

class MainWindow(tk.Tk): 
    def __init__(self): 
     super().__init__() 
     self.title("Main Window") 
     self.geometry("600x400+30+30") 

     tk.Button(self, text = "New Window", command = self.new_window).pack() 
     tk.Button(self, text = "Close Window", command = self.close).pack() 

     self._second_window = None 

    def new_window(self): 
     # This prevents multiple clicks opening multiple windows 
     if self._second_window is not None: 
      return 

     self._second_window = sW.SubWindow(self) 

    def close(self): 
     # Destory the 2nd window and reset the value to None 
     if self._second_window is not None: 
      self._second_window.destroy() 
      self._second_window = None 


if __name__ == '__main__': 
    window = MainWindow() 
    window.mainloop() 

secondWindow.py

import tkinter as tk 

class SubWindow(tk.Toplevel): 
    def __init__(self, master): 
     super().__init__(master) 

     self.title("Sub Window") 
     self.geometry("400x300+30+30") 
     # Change what happens when you click the X button 
     # This is done so changes also reflect in the main window class 
     self.protocol('WM_DELETE_WINDOW', master.close) 

     tk.Button(self, text = "Print", command = self.printMessage).pack() 

    def printMessage(self): 
     print("Wow this actually worked!") 

使用另一個文件一定要沒有你不想要運行的任何全局代碼。你的課程不必繼承TkToplevel,這只是一個例子。但是你需要確保你只有一個Tk的實例,否則你會得到你遇到的行爲

相關問題