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

时间:2012-11-08 18:10:34

标签: 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)
        #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中创建)中单击Edit时,我想要第二个窗口弹出一些其他小部件。此刻我什么也没得到,没有窗户,没有错误,什么都没有。

非常感谢任何帮助。

0 个答案:

没有答案