Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使顶部的子窗口打开_Python_User Interface_Tkinter - Fatal编程技术网

Python 使顶部的子窗口打开

Python 使顶部的子窗口打开,python,user-interface,tkinter,Python,User Interface,Tkinter,我在Tkinter中有一个子窗口(根)。当我打开用于导入数据和处理的新窗口时,该子窗口位于主窗口下 我曾经 root.lift() root.call('wm', 'attributes', '.', '-topmost', True) 但是当目录窗口打开时,子窗口始终保持在顶部 from __future__ import division from Tkinter import * from math import pi import tkMessageBox import Tkinter

我在Tkinter中有一个子窗口(根)。当我打开用于导入数据和处理的新窗口时,该子窗口位于主窗口下

我曾经

root.lift()
root.call('wm', 'attributes', '.', '-topmost', True)
但是当目录窗口打开时,子窗口始终保持在顶部

from __future__ import division
from Tkinter import *
from math import pi
import tkMessageBox
import Tkinter, Tkconstants, tkFileDialog
from numpy import nan
import Tkinter as tk
import os, shutil



class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.filename = None
        self.master.title("input")
        self.master.minsize(250, 150)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)


        for i in range(1): self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.button0 = Button(self, text="start", command=self.open_new_window, activeforeground="red")
        self.button0.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

    def open_new_window(self):

        root = tk.Toplevel(self)
        root.title("process")
        root.minsize(400, 150)
        root.maxsize(400, 150)
        root.grid()

        top = root.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)


        root.CHART_FILE_TYPES = [('All files', '*'),('text file', '.txt')]

        for i in range(5): root.rowconfigure(i, weight=1)
        root.columnconfigure(1, weight=1)

        root.CheckVar_a = IntVar()
        root.check_a = tk.Checkbutton(root, text="A", variable=root.CheckVar_a, onvalue=0, offvalue=1)
        root.check_a.grid(row=0, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        root.CheckVar_b = IntVar()
        root.check_b = tk.Checkbutton(root, text="B", variable=root.CheckVar_b, onvalue=0, offvalue=1)
        root.check_b.grid(row=1, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        root.CheckVar_c = IntVar()
        root.check_c = tk.Checkbutton(root, text="C", variable=root.CheckVar_c, onvalue=0, offvalue=1)
        root.check_c.grid(row=2, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        root.CheckVar_d = IntVar()
        root.check_d = tk.Checkbutton(root, text="D", variable=root.CheckVar_d, onvalue=0, offvalue=1)
        root.check_d.grid(row=3, column=0, pady=0, padx=0, columnspan=3, sticky=W)

        def open_file():
            root.filename = tkFileDialog.askopenfilename(filetypes=[("Text Files",'.txt')])
            if not root.filename:
                tkMessageBox.showerror("Error", message="No text file (*.txt) imported ")
            return root.filename

        open_txt = tk.Button(root, text="Open", command=open_file, activeforeground="red")
        open_txt.grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S)

        def process():
            if not root.filename:
                tkMessageBox.showerror("Error", message="None file (*.txt) active to process")
                return None
            pass

        pro = tk.Button(root, text="Process", command=process, activeforeground="red")
        pro.grid(row=4, column=1, pady=2, padx=2, sticky=E+W+N+S)


if __name__=="__main__":
   d = MainWindow()
   d.mainloop()

你用错了tkinter。程序中不能有多个
Tk
实例。当您在创建根窗口之前创建一个框架时,会隐式地得到一个框架,然后在
open\u new\u window
中得到另一个框架


要创建子窗口,请创建一个
Toplevel

的实例,谢谢Bryan。我需要在MainWindow中创建Toplevel的实例吗?顺便说一句,使用root=tk.Toplevel(self)而不是root=tk()我一直都是这样做的problem@GianniSpear:right:当您创建任何小部件的实例而不首先创建根窗口时,tkinter将自动为您创建根窗口。然后,当你调用
Tk
时,你会得到另一个。这个东西被称为根窗口是有原因的——它是所有其他窗口小部件的后代。Bryan你告诉过我我使用类MainWindow(Frame)的方法吗?@GianniSpear:
MainWindow
很好,但是你应该在创建
MainWindow
之前创建根窗口。另外,
open\u new\u window
也不应创建
Tk
的实例。