前提・実現したいこと
使用言語:Python 3.8.1
buttonを実行して値を取得できているかを確認したいと思っています。
発生している問題・エラーメッセージ
下記のコードの中で下記のエラーメッセージがでています。ウィンドウがボタンモジュールをつける前に消えてしまうということは分かったのですが、解決方法がわからず困っています。
Traceback (most recent call last): File "pokemon.py", line 87, in <module> button() File "pokemon.py", line 78, in button button = tk.Button(main.win, text = 'print', command = lambda : [ap.get_info(), dp.get_info()]) File "C:\Users\gojin\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2645, in __init__ Widget.__init__(self, master, 'button', cnf, kw) File "C:\Users\gojin\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__ self.tk.call( _tkinter.TclError: can't invoke "button" command: application has been destroyed
該当のソースコード
Python
1import tkinter as tk 2import tkinter.ttk as ttk 3import sqlite3 as sql 4 5import main 6import cursor 7 8main 9cursor 10 11class AttackingPokemon: 12 13 def __init__(self, name, type1, type2, lv, iv, ev, nature, status, A, C, S): 14 15 self.name = name 16 self.type1 = type1 17 self.type2 = type2 18 self.lv = lv 19 self.iv = iv 20 self.ev = ev 21 self.nature = nature 22 self.status = status 23 self.A = A 24 self.C = C 25 self.S = S 26 27 def get_info(self): 28 29 print(self.name.get()) 30 print(self.type1.get()) 31 print(self.type2.get()) 32 print(self.lv.get()) 33 print(self.iv.get()) 34 print(self.ev.get()) 35 print(self.nature.get()) 36 print(self.status) 37 print(self.A) 38 print(self.C) 39 print(self.S) 40 41class DefendingPokemon: 42 43 def __init__(self, name, type1, type2, lv, iv, ev, nature, status, H, B, D, S): 44 45 self.name = name 46 self.type1 = type1 47 self.type2 = type2 48 self.lv = lv 49 self.iv = iv 50 self.ev = ev 51 self.nature = nature 52 self.status = status 53 self.H = H 54 self.B = B 55 self.D = D 56 self.S = S 57 58 def get_info(self): 59 60 print(self.name.get()) 61 print(self.type1) 62 print(self.type2) 63 print(self.lv.get()) 64 print(self.iv.get()) 65 print(self.ev.get()) 66 print(self.nature.get()) 67 print(self.status) 68 print(self.H) 69 print(self.B) 70 print(self.D) 71 print(self.S) 72 73def button(): 74 75 ap = AttackingPokemon(main.aname, cursor.at1, cursor.at2, main.alventry, main.aiventry, main.aeventry, main.anaturecheck, '-', cursor.A, cursor.C, cursor.aS) 76 dp = DefendingPokemon(main.dname, cursor.dt1, cursor.dt2, main.dlventry, main.diventry, main.deventry, main.dnaturecheck, '-', cursor.H, cursor.B, cursor.D, cursor.dS) 77 78 button = tk.Button(main.win, text = 'print', command = lambda : [ap.get_info(), dp.get_info()]) 79 button.pack() 80 81 main.win.mainloop() 82 83if __name__ == "__main__": 84 85 main 86 main.win.mainloop() 87 button()
Python
1#main.py 2 3import tkinter as tk 4import tkinter.ttk as ttk 5 6win = tk.Tk() 7win.title('ダメージ計算') 8win.geometry('500x600') 9 10atkframe = tk.Frame(win) 11atkframe.pack(pady = 5) 12 13nameframe = tk.Frame(atkframe) 14nameframe.pack(pady = 5) 15alabel = tk.Label(nameframe, text = "攻撃側 ") 16alabel.pack(side = 'left') 17aname = tk.Entry(nameframe, name = 'aname') 18aname.pack(side = 'left') 19aname.insert(index = 'insert', string = "インテレオン") 20 21lvframe = tk.Frame(atkframe) 22lvframe.pack(pady = 5) 23alvlabel = tk.Label(lvframe, text = "レベル ") 24alvlabel.pack(side = 'left') 25alventry = tk.Entry(lvframe) 26alventry.pack(side = 'left') 27alventry.insert(index = 'insert', string = '50') 28 29idframe = tk.Frame(atkframe) 30idframe.pack(pady = 5) 31aivlabel = tk.Label(idframe, text = "個体値 ") 32aivlabel.pack(side = 'left', ipadx = 5) 33aiventry = tk.Entry(idframe) 34aiventry.pack(side = 'left') 35aiventry.insert(index = 'insert', string = '31') 36 37aevlabel = tk.Label(idframe, text = "努力値 ") 38aevlabel.pack(side = 'left') 39aeventry = tk.Entry(idframe) 40aeventry.pack(side = 'left', ipadx = 5) 41aeventry.insert(index = 'insert', string = '252') 42 43natureframe = tk.Frame(atkframe) 44natureframe.pack(pady = 8) 45anaturelabel = tk.Label(natureframe, text = "性格 ") 46anaturelabel.pack(side = 'left') 47anaturecheck = ttk.Combobox(natureframe, state = 'readonly') 48anaturecheck['values'] = ("", "さみしがり", "いじっぱり", "やんちゃ", "ゆうかん", "ずぶとい", "わんぱく", "のうてんき", "のんき", "ひかえめ", "おっとり", "うっかりや", "れいせい", "おだやか", "おとなしい", "しんちょう", "なまいき", "おくびょう", "せっかち", "ようき", "むじゃき", "てれや", "がんばりや", "すなお", "きまぐれ", "まじめ") 49anaturecheck.current(0) 50anaturecheck.pack(side = 'left') 51 52moveframe = tk.Frame(atkframe) 53moveframe.pack() 54mlabel = tk.Label(moveframe, text = "わざ名 ") 55mlabel.pack() 56mname = tk.Entry(moveframe) 57mname.pack() 58mname.insert(index = 'insert', string = "") 59 60defframe = tk.Frame(win) 61defframe.pack(pady = 15) 62 63nameframe = tk.Frame(defframe) 64nameframe.pack(pady = 5) 65dlabel = tk.Label(nameframe, text = "防御側 ") 66dlabel.pack(side = 'left') 67dname = tk.Entry(nameframe) 68dname.pack(side = 'left') 69dname.insert(index = 'insert', string = "エースバーン") 70 71lvframe = tk.Frame(defframe) 72lvframe.pack(pady = 5) 73dlvlabel = tk.Label(lvframe, text = "レベル ") 74dlvlabel.pack(side = 'left') 75dlventry = tk.Entry(lvframe) 76dlventry.pack(side = 'left') 77dlventry.insert(index = 'insert', string = '50') 78 79idframe = tk.Frame(defframe) 80idframe.pack(pady = 5) 81divlabel = tk.Label(idframe, text = "個体値 ") 82divlabel.pack(side = 'left', ipadx = 5) 83diventry = tk.Entry(idframe) 84diventry.pack(side = 'left') 85diventry.insert(index = 'insert', string = '31') 86 87devlabel = tk.Label(idframe, text = "努力値 ") 88devlabel.pack(side = 'left') 89deventry = tk.Entry(idframe) 90deventry.pack(side = 'left', ipadx = 5) 91deventry.insert(index = 'insert', string = '252') 92 93natureframe = tk.Frame(defframe) 94natureframe.pack(pady = 8) 95dnaturelabel = tk.Label(natureframe, text = "性格 ") 96dnaturelabel.pack(side = 'left') 97dnaturecheck = ttk.Combobox(natureframe) 98dnaturecheck['values'] = ("", "さみしがり", "いじっぱり", "やんちゃ", "ゆうかん", "ずぶとい", "わんぱく", "のうてんき", "のんき", "ひかえめ", "おっとり", "うっかりや", "れいせい", "おだやか", "おとなしい", "しんちょう", "なまいき", "おくびょう", "せっかち", "ようき", "むじゃき", "てれや", "がんばりや", "すなお", "きまぐれ", "まじめ") 99dnaturecheck.current(0) 100dnaturecheck.pack(side = 'left')
Python
1#cursor.py 2 3import tkinter as tk 4import sqlite3 as sql 5import tkinter.ttk as ttk 6 7import main 8 9main 10 11dbpath = "dex.sqlite3" 12conn = sql.connect(dbpath) 13cur = conn.cursor() 14 15cur.execute("SELECT type1 FROM dex WHERE name = '%s'" % main.aname.get()) 16at1 = cur.fetchone() 17cur.execute("SELECT type2 FROM dex WHERE name = '%s'" % main.aname.get()) 18at2 = cur.fetchone() 19cur.execute("SELECT A FROM dex WHERE name = '%s'" % main.aname.get()) 20A = cur.fetchone() 21cur.execute("SELECT C FROM dex WHERE name = '%s'" % main.aname.get()) 22C = cur.fetchone() 23cur.execute("SELECT S FROM dex WHERE name = '%s'" % main.aname.get()) 24aS = cur.fetchone() 25 26cur.execute("SELECT type1 FROM dex WHERE name = '%s'" % main.dname.get()) 27dt1 = cur.fetchone() 28cur.execute("SELECT type2 FROM dex WHERE name = '%s'" % main.aname.get()) 29dt2 = cur.fetchone() 30cur.execute("SELECT H FROM dex WHERE name = '%s'" % main.dname.get()) 31H = cur.fetchone() 32cur.execute("SELECT B FROM dex WHERE name = '%s'" % main.dname.get()) 33B = cur.fetchone() 34cur.execute("SELECT D FROM dex WHERE name = '%s'" % main.dname.get()) 35D = cur.fetchone() 36cur.execute("SELECT S FROM dex WHERE name = '%s'" % main.dname.get()) 37dS = cur.fetchone() 38 39cur.execute("SELECT type FROM moves WHERE name = '%s'" % main.mname.get()) 40mtype = cur.fetchone() 41cur.execute("SELECT mtype FROM moves WHERE name = '%s'" % main.mname.get()) 42mmtype = cur.fetchone() 43cur.execute("SELECT power FROM moves WHERE name = '%s'" % main.mname.get()) 44mpower = cur.fetchone() 45 46cur.execute("SELECT rev FROM types WHERE type1 = '%s' AND type2 = '%s'" % (mtype, dt1)) 47rev1 = cur.fetchone() 48cur.execute("SELECT rev FROM types WHERE type1 = '%s' AND type2 = '%s'"% (mtype, dt2)) 49rev2 = cur.fetchone() 50 51main.win.mainloop() 52 53if __name__ == "__main__": 54 55 main 56 main.win.mainloop()
試したこと
buttonモジュールのcommandを片方のみにするなど、変更をしましたが、上記と全く同じエラーコードが出ました。
補足
cursor.pyの中でもrev1のほうでsyntax errorが出ています。図々しいですが、よければこちらも教えてもらえればありがたいです。
mainやcursorというモジュールが不明なため提示エラーが再現できませんので提示ください。
回答2件
あなたの回答
tips
プレビュー