Python 从另一个窗口打开新的tkinter窗口

Python 从另一个窗口打开新的tkinter窗口,python,user-interface,tkinter,Python,User Interface,Tkinter,我试图设计一个基本的界面,但遇到了一些麻烦 代码: ServerMainWindow.py import Tkinter as tk import ServerEditWindow as editWindow class ServerMainWindow(tk.Toplevel): def __init__(self, parent, title = None): tk.Toplevel.__init__(self, parent) #associa

我试图设计一个基本的界面,但遇到了一些麻烦

代码:

ServerMainWindow.py

import Tkinter as tk

import ServerEditWindow as editWindow

class ServerMainWindow(tk.Toplevel):
    def __init__(self, parent, title = None):

        tk.Toplevel.__init__(self, parent)
        #associate this window with a parent window
        self.transient(parent)

        if title:
            self.title(title)

        self.parent = parent

        self.result = None

        body = tk.Frame(parent)
        self.initial_focus = self.listbox(body)
        body.pack(fill='both')

        self.buttons()

        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = self

        self.protocol("WM_DELETE_WINDOW", self.cancel)

        self.initial_focus.focus_set()

        self.wait_window(self)

    def listbox(self, parent):
        """
        create the listbox that will hold the available job names.
        the Listbox widget will have initial focus when the dialog
        is created
        """

        self.listbox = tk.Listbox(parent)
        self.listbox.pack(fill='both')

        for item in [1, 2, 3, 4]:
            self.listbox.insert(tk.END, item)

        return self.listbox

    def buttons(self):
        """
        add a frame to hold the buttons
        Edit, Stop, and Stop all
        """

        box = tk.Frame(self)

        edit = tk.Button(box, text="Edit", width=10, command=self.edit)
        edit.pack(side=tk.LEFT, padx=5, pady=5)

        stop = tk.Button(box, text="Stop", width=10, command=self.stop)
        stop.pack(side=tk.LEFT, padx=5, pady=5)

        stopAll = tk.Button(box, text="Stop all", width=10, command=self.stopAll)
        stopAll.pack(side=tk.LEFT, padx=5, pady=5)

        box.pack()

    def edit(self):
        """
        callback function for the edit button

        will create a new dialog window with the access list
        for the selected job (from the Listbox in self.listbox)
        """

        d = editWindow.ServerEditWindow(self.parent, title="other window")

    def stop(self):
        """
        callback function for the stop button

        will stop the headless sds2 process for the selected job
        (from the Listbox in self.listbox)
        """

        pass

    def stopAll(self):
        """
        callback function for the stop all button

        will stop all running headless sds2 processes
        """

        pass

    def cancel(self, even=None):
        self.parent.focus_set()
        self.destroy()
ServerEditWindow.py

import Tkinter as tk

class ServerEditWindow(tk.Toplevel):
    def __init__(self, parent, title = None):
        tk.Toplevel.__init__(self, parent)

        #associate this window with the parent
        self.transient(parent)

        if title:
            self.title(title)

        body = tk.Frame(parent)
        self.initial_focus = self.listbox(body)
        body.pack(padx=5, pady=5)

        self.parent = parent

        self.result = None

        self.newUser(body)
        self.buttons()

        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = self

        self.protocol("WM_DELETE_WINDOW", self.cancel)

        self.initial_focus.focus_set()

        self.wait_window(self)

    def listbox(self, parent):
        """
        create a listbox to hold the users with access to the currently selected job
        """

        self.listbox = tk.Listbox(parent)
        self.listbox.pack()

        return self.listbox

    def newUser(self, parent):
        """
        a frame that holds an entry widget and a button to confirm the
        addition of a user to the list of users who canaccess the job info.
        """

        box = tk.Frame(parent)

        self.entry = tk.Entry(box)
        self.entry.pack(side = tk.TOP)

        button = tk.Button(box, text="Add user", callback = self.addUser)
        button.pack(side=tk.TOP)

        box.pack()

    def buttons(self):
        """
        create the delete button for the Listbox.
        """

        box = tk.Frame(self)

        delete = tk.Button(box, text = "Delete user", callback=self.deleteUser)
        delete.pack(side=tk.LEFT, padx=5, pady=5)

    def addUser(self):
        """
        add the user from self.entry to the list of users able to access
        the job information.
        """

        pass

    def deleteUser(self):
        """
        delete the user currently selected in the Listbox.
        """

        pass


    def cancel(self, even=None):
        self.parent.focus_set()
        self.destroy()
以及用于测试的脚本:

#!/usr/bin/python

import ServerGUI as gui
import Tkinter as tk

root = tk.Tk()

d = gui.ServerMainWindow(root, title="main test")

root.mainloop()
问题是:当从第一个对话框(在ServerMainWindow中创建)单击编辑时,我希望第二个窗口弹出一些其他小部件。现在我什么都没有,没有窗口,没有错误,什么都没有


非常感谢您的帮助。

我复制并粘贴了您的代码,单击“编辑”按钮打开了新窗口。你是说这不会发生在你身上?我正在逐字运行您的代码,只是我将所有代码放在一个文件中(并修复了与不单独导入文件相关的两个错误),在您的两个
\uuuuu init\uuuuu
中,
body=tk.Frame(parent)
可能不是您想要的。代码所做的是创建一个空窗口(
root
),在创建空窗口时将其填入
ServerMainWindow()
。然后在每个
edit
操作中,创建一个新的空窗口,并将小部件放入在
ServerMainWindow()中创建的窗口中。
。。。