2016-03-30 127 views
0

私はメモを取るアプリケーションを作っています(Windowsの付箋に似ています)。複数の音符を同時に表示する必要があるので、私はThreadから継承したクラスを使用し、tkinterウィンドウも作成しました。問題は、私のウィンドウが同時に開かないということです。 2番目は最初のものが閉じられた後に開きます。ここにコードがあります。私は間違って何をしていますか?私が使用できる別の方法がありますか? [今の私はちょうど私がハードコーディングされているノートを表示しています。]Pythonで複数のtkinterウィンドウを同時に実行するにはどうしたらいいですか?

from tkinter import * 
from threading import Thread 

class Note(Thread): 
nid = 0 
title = "" 
message = "" 

    def __init__(self, nid, title, message): 
     Thread.__init__(self) 
     self.nid = nid 
     self.title = title 
     self.message = message 


    def display_note_gui(self): 
     '''Tkinter to create a note gui window with parameters '''  
     window = Tk() 
     window.title(self.title) 
     window.geometry("200x200") 
     window.configure(background="#BAD0EF") 

     title = Entry(relief=FLAT, bg="#BAD0EF", bd=0) 
     title.pack(side=TOP) 
     scrollBar = Scrollbar(window, takefocus=0, width=20) 
     textArea = Text(window, height=4, width=1000, bg="#BAD0EF", font=("Times", "14")) 
     scrollBar.pack(side=RIGHT, fill=Y) 
     textArea.pack(side=LEFT, fill=Y) 
     scrollBar.config(command=textArea.yview) 
     textArea.config(yscrollcommand=scrollBar.set) 
     textArea.insert(END, self.message) 
     window.mainloop() 

    def run(self): 
     self.display_note_gui() 

new_note1 = Note(0, "Hello", "Hi, how are you?") 
new_note1.start() 
new_note1.join() 


new_note2 = Note(1, "2", "How's everyone else?") 
new_note2.start() 
new_note2.join() 
+0

'Toplevel'ウィジェットを使用しますか? –

答えて

0

代わりのサブクラス化ThreadがちょうどサブクラスToplevel、Tkinterではトップレベルは、あなたが達成しようとしている正確に何のように聞こえる、同じアプリケーションで別のウィンドウである:

from tkinter import * 
#from threading import Thread #no longer needed 

class Note(Toplevel): 
    nid = 0 
    #title = "" #this would block the method to override the current title 
    message = "" 

    def __init__(self, master, nid, title, message): 
     Toplevel.__init__(self,master) 
     self.nid = nid 
     self.title(title) #since toplevel widgets define a method called title you can't store it as an attribute 
     self.message = message 
     self.display_note_gui() #maybe just leave that code part of the __init__? 


    def display_note_gui(self): 
     '''Tkinter to create a note gui window with parameters '''  
     #no window, just self 
     self.geometry("200x200") 
     self.configure(background="#BAD0EF") 
     #pass self as the parent to all the child widgets instead of window 
     title = Entry(self,relief=FLAT, bg="#BAD0EF", bd=0) 
     title.pack(side=TOP) 
     scrollBar = Scrollbar(self, takefocus=0, width=20) 
     textArea = Text(self, height=4, width=1000, bg="#BAD0EF", font=("Times", "14")) 
     scrollBar.pack(side=RIGHT, fill=Y) 
     textArea.pack(side=LEFT, fill=Y) 
     scrollBar.config(command=textArea.yview) 
     textArea.config(yscrollcommand=scrollBar.set) 
     textArea.insert(END, self.message) 
     #self.mainloop() #leave this to the root window 

    def run(self): 
     self.display_note_gui() 

root = Tk() 
root.withdraw() #hide the root so that only the notes will be visible 

new_note1 = Note(root, 0, "Hello", "Hi, how are you?") 
#new_note1.start() 
#new_note1.join() 


new_note2 = Note(root, 1, "2", "How's everyone else?") 
#new_note2.start() 
#new_note2.join() 

root.mainloop() #still call mainloop on the root 

属性としてタイトルを格納する代わりに、self.title()を呼び出してウィンドウの現在のタイトルを取得し、self.title("new title")に変更することができます。

+0

ありがとう、これは働いた!タイトルに関する提案をお寄せいただきありがとうございます。私は非常にPythonには新しく、私はそれを非常によく理解していました。 – Me95

+0

通常、 'title'という属性を使用すると問題なく、コードについて変更する必要がありました'Toplevel'のサブクラスと互換性を持たせるために –

+0

をテストしていたときに' str object is not callable'エラーが発生していなければ、それをそのまま残しておきました。 :) –

1

あなたが必要なのは、複数のノートウィンドウが、あなたは間違いなくスレッドを必要としない場合には。 Tkinterは数十〜数百のオープンウィンドウを管理することができます。

ルートウィンドウ以外のすべてのウィンドウに対して、Toplevelのインスタンスを作成するだけです。ここでは、やや過剰設計例です:

import Tkinter as tk 

class Notepad(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 
     self.text = tk.Text(self, wrap="word") 
     self.vsb = tk.Scrollbar(self, orient="vertical", comman=self.text.yview) 
     self.text.configure(yscrollcommand=self.vsb.set) 
     self.vsb.pack(side="right", fill="y") 
     self.text.pack(side="left", fill="both", expand=True) 

def main(): 
    root = tk.Tk() 
    Notepad(root).pack(fill="both", expand=True) 
    for i in range(5): 
     top = tk.Toplevel(root) 
     Notepad(top).pack(fill="both", expand=True) 

    root.mainloop() 

if __name__ == "__main__": 
    main() 
関連する問題