2016-08-12 12 views
1
from tkinter import * 

class Main(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent   
     self.initUI() 
    def initUI(self): 
     global canvas 
     self.parent.title('Python') 
     self.pack(fill = BOTH, expand = 1) 
     canvas = Canvas(self) 
     self.Label_My = Label(self, text = 'MyObject') 
     self.Label_My.place(x = 0, y = 0) 

     canvas.pack(fill = BOTH, expand = 1) 
     canvas.update() 
class Main2(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent   
     self.initUI() 
    def initUI(self): 
     global canvas 
     self.parent.title('Python') 
     self.pack(fill = BOTH, expand = 1) 
     canvas = Canvas(self) 
     self.Label_My = Label(self, text = 'MyObject2') 
     self.Label_My.place(x = 0, y = 0) 

    canvas.pack(fill = BOTH, expand = 1) 
    canvas.update() 

root = Tk() 
ex = Main(root) 
root.geometry('700x500') 

root2 = Tk() 
ex2 = Main2(root2) 
root2.geometry('500x500') 

def d(): 
    if root2: 
     root2.destroy() 
    if root: 
     root.destroy() 

私は2つのtkinterウィンドウを作成しましたが、存在する場合は閉じますが、作成しないと "root/root2"は印刷されません窓として。Python - tkinterでウィンドウが閉じていない場合

また、「ルート」を最初に閉じる必要があることがわかりました。 "root2"を閉じると、 "pythonw.exeが動作を停止しました"というメッセージが表示されます。

私の解決方法は "rootxの場合"の前に "try-except-statement"を追加する方法です。

+0

'' 'Main''と' 'Main2'''の完全なコード定義を表示できますか? '' 'd()' ''はどこで呼び出されますか? –

+0

2つのルートウィンドウを持つtkinterプログラムは、あなたが思うように動作することは期待できません。 tkinterプログラムはちょうど1つのルートウィンドウを持つ必要があります。そのため、それは_root_ウィンドウと呼ばれています。複数のウィンドウが必要な場合は、 'Toplevel'のインスタンスを作成します。 –

+0

私は今すべてのコードを示しました。 – Montague27

答えて

0

作成したウィンドウをリストに追加し、追加した順序でウィンドウを破棄します。

windows = [] 

root = Tk() 
ex = Main(root) 
root.geometry('700x500') 
windows.append(root) 

root2 = Tk() 
ex2 = Main2(root2) 
root2.geometry('500x500') 
windows.append(root2) 

def d(): 
    for window in windows: 
     window.destroy() 
関連する問題