我有一个子窗口(根)在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()
你用错了。在程序中不能有一个以上的Tk
实例。当您在创建根窗口之前创建框架时,您会隐式地获得一个,然后在open_new_window
中获得另一个。
创建子窗口,创建Toplevel
的实例