前提・実現したいこと
使用言語:Python 3.8.1
使用データベース:SQLite3 3.30.1
発生している問題・エラーメッセージ
下記ソースのうち、main.aname.get()で取得したエントリーの値に対応するA, C, Sの値がprint(self.a), print(self.c), print(self.s)で出力された際に反映されません。
該当のソースコード
Python
1# -*- coding: utf-8 -*- 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 15class AttackDatabase(): 16 17 def __init__(self, name, a, c, s): 18 19 self.name = name 20 self.a = a 21 self.c = c 22 self.s = s 23 24 def get_info(self): 25 26 cur.execute("SELECT A FROM dex WHERE name = '%s'" % main.aname.get()) 27 A = cur.fetchone() 28 cur.execute("SELECT C FROM dex WHERE name = '%s'" % main.aname.get()) 29 C = cur.fetchone() 30 cur.execute("SELECT S FROM dex WHERE name = '%s'" % main.aname.get()) 31 S = cur.fetchone() 32 33 self.a = A[0] 34 self.c = C[0] 35 self.s = S[0] 36 37 print(self.name.get()) 38 print(self.a) 39 print(self.c) 40 print(self.s) 41 42button = tk.Button(main.win, text = 'print', command = AttackDatabase.get_info) 43button.pack() 44 45main.win.mainloop()
Python
1#main.py 2 3import tkinter as tk 4import tkinter.ttk as ttk 5 6win = tk.Tk() 7win.title('ダメージ計算') 8win.geometry('500x500') 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')
試したこと
SELECT文周辺の位置を変えるといったことを試しましたが、できませんでした。
追記:以下のコードに変更した場合に、数値は出力され、main.aname.get()をエントリーの変更で反映されましたが、self.a, self.c, self.sの三つについては初期値のまま出力されました。
Python
1import tkinter as tk 2import sqlite3 as sql 3import tkinter.ttk as ttk 4 5import main 6 7main 8 9dbpath = "dex.sqlite3" 10conn = sql.connect(dbpath) 11cur = conn.cursor() 12 13cur.execute("SELECT A FROM dex WHERE name = '%s'" % main.aname.get()) 14A = cur.fetchone() 15cur.execute("SELECT C FROM dex WHERE name = '%s'" % main.aname.get()) 16C = cur.fetchone() 17cur.execute("SELECT S FROM dex WHERE name = '%s'" % main.aname.get()) 18S = cur.fetchone() 19 20 21class AttackDatabase(): 22 23 def __init__(self, name, a, c, s): 24 25 self.name = name 26 self.a = a 27 self.c = c 28 self.s = s 29 30 def get_info(self): 31 32 print(self.name.get()) 33 print(self.a) 34 print(self.c) 35 print(self.s) 36 37ad = AttackDatabase(main.aname, A, C, S) 38 39button = tk.Button(main.win, text = 'print', command = ad.get_info) 40button.pack() 41 42main.win.mainloop() 43 44if __name__ == "__main__": 45 46 main 47 main.win.mainloop()
回答1件
あなたの回答
tips
プレビュー