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"を追加する方法です。
'' 'Main''と' 'Main2'''の完全なコード定義を表示できますか? '' 'd()' ''はどこで呼び出されますか? –
2つのルートウィンドウを持つtkinterプログラムは、あなたが思うように動作することは期待できません。 tkinterプログラムはちょうど1つのルートウィンドウを持つ必要があります。そのため、それは_root_ウィンドウと呼ばれています。複数のウィンドウが必要な場合は、 'Toplevel'のインスタンスを作成します。 –
私は今すべてのコードを示しました。 – Montague27