我在Tkinter有一个子窗口(root)。当我打开导入数据和处理的新窗口时,这个子窗口进入主窗口。
我用过
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()
答案 0 :(得分:1)
您使用tkinter错误。您的程序中不能有多个Tk
实例。在创建根窗口之前创建框架时会隐式得到一个,然后在open_new_window
中得到另一个框架。
要创建子窗口,请创建Toplevel
。