简体   繁体   English

Python:Tkinter文本小部件,新窗口按钮

[英]Python: Tkinter text widget, new window button

I'm making a tkinter based text widget and I'm trying to implement a new window function in it. 我正在制作一个基于tkinter的文本小部件,并且试图在其中实现一个新的窗口功能。 But every time I click on the new window button I get this error in IDLE : RuntimeError: Calling Tcl from different appartment 但是,每次我单击新窗口按钮时,都会在IDLE中收到此错误:RuntimeError:从其他位置调用Tcl

Here's my code : 这是我的代码:

#!/usr/bin/env python
from Tkinter import *
from tkSimpleDialog import askstring
from tkFileDialog   import asksaveasfilename
from tkFileDialog import askopenfilename
from tkMessageBox import askokcancel
import Tkinter as tk
import ttk 
import threading
from ScrolledText import ScrolledText
Window = Tk() 
Window.title("TekstEDIT")

/..CODE.../

class newWindowThread(threading.Thread):
    def __init__(self, choosen=""):
        threading.Thread.__init__(self)
        self.choosen = choosen
    def run(self):
        if self.choosen == "":
            root = Tk()
            newEditor = SimpleEditor(root)
            root.mainloop()
        else:
            root = Tk()
            newEditor = SimpleEditor(root, self.choosen)
            root.mainloop()
/...CODE.../
wFile = Menu(menubar, tearoff=0,relief="raised")
    wFile.add_command(label="New", accelerator="Ctrl+N", command=self.onNew)
    wFile.add_command(label="New Window", accelerator="Ctrl+Shift+N", command=self.onNewWindow)
/...CODE.../
def onNewWindow(self):
    t=newWindowThread()
    t.start()

What's the source of the problem? 问题的根源是什么? How can I solve it? 我该如何解决?

Source Code: http://ideone.com/npWuYD 源代码: http : //ideone.com/npWuYD

Thanks. 谢谢。

You can't mix Tkinter and threading in this way. 您不能以这种方式混合使用Tkinter和线程。 Tkinter isn't thread-safe. Tkinter不是线程安全的。 Also, tkinter is designed such that you should only ever have a single instance of Tk running. 同样,tkinter的设计使您只能运行一个Tk实例。

If you want multiple windows, you don't need threads and you don't need multiple instances of Tk . 如果您需要多个窗口,则不需要线程,也不需要多个Tk实例。 Create your root window as usual, and for other windows create an instance of Toplevel . 照常创建根窗口,并为其他窗口创建Toplevel实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM