質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

635閲覧

【Python】tk.Entry()から取得した内容が反映されません。

JinGoda

総合スコア20

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/02/14 16:30

前提・実現したいこと

何度も質問失礼します。
下記のdamagecalculation.pyを実行した際に、cursor.pyでmain.aname.get()で取得したH, A, B, C, Dの値を利用して計算したいと思っています(今はH, A, Bの三つを使ってテストしています)。
これを実行した際に取得はできているのですが、main.anameの文字列とmain.dnameの文字列を入れ替えた際に取得する値(H, A, B)が入れ替え前と全く同じになっており、最初に取得したものに固定されているようです。

発生している問題・エラーメッセージ

エラーメッセージは出ませんが、攻撃側と防御側の入れ替わった値が得られず、計算がうまくいきません。

該当のソースコード

Python

1#calculation.py 2def calc(): 3 4 import cursor 5 import field 6 import pokemon 7 import revs 8 9 cur.execute("SELECT type FROM moves WHERE name = '%s'" % main.mname.get()) 10 mtype = cur.fetchone() 11 cur.execute("SELECT rev FROM types WHERE type1 = '%s' AND type2 = '%s'" % (mtype[0], cursor.dt1[0])) 12 rev1 = cur.fetchone() 13 cur.execute("SELECT rev FROM types WHERE type1 = '%s' AND type2 = '%s'"% (mtype[0], cursor.dt2[0])) 14 rev2 = cur.fetchone() 15 cur.execute("SELECT power FROM moves WHERE name = '%s'" % main.mname.get()) 16 mpower = cur.fetchone() 17 cur.execute("SELECT a FROM nature WHERE nature = '%s'" % main.anaturecheck.get()) 18 na = cur.fetchone() 19 cur.execute("SELECT b FROM nature WHERE nature = '%s'" % main.dnaturecheck.get()) 20 nb = cur.fetchone() 21 cur.execute("SELECT c FROM nature WHERE nature = '%s'" % main.anaturecheck.get()) 22 nc = cur.fetchone() 23 cur.execute("SELECT d FROM nature WHERE nature = '%s'" % main.dnaturecheck.get()) 24 nd = cur.fetchone() 25 typecomp = rev1[0] * rev2[0] 26 27 H = int(int(pokemon.dp.H * 2 + int(pokemon.dp.iv) + int(pokemon.dp.ev) / 4) * int(pokemon.dp.lv) / 100 + int(pokemon.dp.lv) + 10) 28 A = int(int(int(pokemon.ap.A * 2 + int(pokemon.ap.iv) + int(pokemon.ap.ev) / 4) * int(pokemon.ap.lv) / 100 + 5) * na[0]) 29 B = int(int(int(pokemon.dp.B * 2 + int(pokemon.dp.iv) + int(pokemon.dp.ev) / 4) * int(pokemon.dp.lv) / 100 + 5) * nb[0]) 30 C = int(int(int(pokemon.ap.C * 2 + int(pokemon.ap.iv) + int(pokemon.ap.ev) / 4) * int(pokemon.ap.lv) / 100 + 5) * nc[0]) 31 D = int(int(int(pokemon.dp.D * 2 + int(pokemon.ap.iv) + int(pokemon.ap.ev) / 4) * int(pokemon.ap.lv) / 100 + 5) * nd[0]) 32 33 if cursor.mmtype[0] == 'A': 34 35 f = math.floor(math.floor(math.floor(int(pokemon.ap.lv) * 2 / 5 + 2) * mpower[0] * A / B) / 50 + 2) * revs.ran() 36 s = str(f) 37 a, b = s.split('.') 38 39 if int(b) > 5: 40 41 c = math.ceil(f) 42 43 else: 44 45 c = math.floor(f) 46 47 f2 = c * field.rain() 48 s2 = str(f2) 49 a2, b2 = s2.split('.') 50 51 if int(b2) > 5: 52 53 d = math.ceil(f2) 54 55 else: 56 57 d = math.floor(f2) 58 59 f3 = d * field.sun() 60 s3 = str(f3) 61 a3, b3 = s3.split('.') 62 63 if int(b3) > 5: 64 65 e = math.ceil(f3) 66 67 else: 68 69 e = math.floor(f3) 70 71 f4 = e * revs.critical() 72 s4 = str(f4) 73 a4, b4 = s4.split('.') 74 75 if int(b4) > 5: 76 77 g = math.ceil(f4) 78 79 else: 80 81 g = math.floor(f4) 82 83 f5 = g * 85 / 100 84 f6 = g * 100/ 100 85 h1 = math.floor(f5) 86 h2 = math.floor(f6) 87 88 f7 = h1 * revs.match() 89 f8 = h2 * revs.match() 90 s5 = str(f7) 91 s6 = str(f8) 92 a5, b5 = s5.split('.') 93 a6, b6 = s6.split('.') 94 95 if int(b5) > 5: 96 97 i1 = math.ceil(f7) 98 99 else: 100 101 i1 = math.floor(f7) 102 103 if int(b6) > 5: 104 105 i2 = math.ceil(f8) 106 107 else: 108 109 i2 = math.floor(f8) 110 111 f9 = i1 * typecomp 112 f10 = i2 * typecomp 113 j1 = math.floor(f9) 114 j2 = math.floor(f10) 115 116 f11 = j1 * revs.burn() 117 f12 = j2 * revs.burn() 118 s7 = str(f11) 119 s8 = str(f12) 120 a7, b7 = s7.split('.') 121 a8, b8 = s8.split('.') 122 123 if int(b7) > 5: 124 125 k1 = math.ceil(f11) 126 127 else: 128 129 k1 = math.floor(f11) 130 131 if int(b8) > 5: 132 133 k2 = math.ceil(f12) 134 135 else: 136 137 k2 = math.floor(f12) 138 139 dmg = k1 140 141 Dmg = k2 142 143 if dmg == 0: 144 145 dmg = 1 146 147 if Dmg == 0: 148 149 dmg = 1 150 151 hpleft = H - dmg 152 HPleft = H - Dmg 153 154 if H % Dmg >= dmg and H % Dmg < Dmg: 155 156 per = (Dmg - (H % dmg)) / (Dmg - dmg) 157 print('乱数' + str(H // dmg)) 158 print(per + '%') 159 160 elif H % dmg < dmg: 161 162 print ('確定' + str(H // dmg + 1)) 163 164 else: 165 166 print('error') 167 168 print(str(int(dmg)) + '~' + str(int(Dmg))) 169 print(typecomp) 170 print(H) 171 print(B) 172 print(hpleft) 173 print(HPleft)

Python

1#pokemon.py 2class AttackingPokemon: 3 4 def __init__(self, name, type1, type2, lv, iv, ev, nature, A, C, S): 5 6 self.name = name.get() 7 self.type1 = type1 8 self.type2 = type2 9 self.lv = lv.get() 10 self.iv = iv.get() 11 self.ev = ev.get() 12 self.nature = nature.get() 13 self.A = A 14 self.C = C 15 self.S = S 16 17class DefendingPokemon: 18 19 def __init__(self, name, type1, type2, lv, iv, ev, nature, H, B, D, S): 20 21 self.name = name.get() 22 self.type1 = type1 23 self.type2 = type2 24 self.lv = lv.get() 25 self.iv = iv.get() 26 self.ev = ev.get() 27 self.nature = nature.get() 28 self.H = H 29 self.B = B 30 self.D = D 31 self.S = S 32 33ap = AttackingPokemon(main.aname, cursor.at1[0], cursor.at2[0], main.alventry, main.aiventry, main.aeventry, main.anaturecheck, cursor.A[0], cursor.C[0], cursor.aS[0]) 34dp = DefendingPokemon(main.dname, cursor.dt1[0], cursor.dt2[0], main.dlventry, main.diventry, main.deventry, main.dnaturecheck, cursor.H[0], cursor.B[0], cursor.D[0], cursor.dS[0])

Python

1#cursor.py 2dbpath = "dex.sqlite3" 3conn = sql.connect(dbpath) 4cur = conn.cursor() 5 6cur.execute("SELECT type1 FROM dex WHERE name = '%s'" % main.aname.get()) 7at1 = cur.fetchone() 8cur.execute("SELECT type2 FROM dex WHERE name = '%s'" % main.aname.get()) 9at2 = cur.fetchone() 10 11cur.execute("SELECT S FROM dex WHERE name = '%s'" % main.aname.get()) 12aS = cur.fetchone() 13 14cur.execute("SELECT type1 FROM dex WHERE name = '%s'" % main.dname.get()) 15dt1 = cur.fetchone() 16cur.execute("SELECT type2 FROM dex WHERE name = '%s'" % main.aname.get()) 17dt2 = cur.fetchone() 18cur.execute("SELECT H FROM dex WHERE name = '%s'" % main.dname.get()) 19H = cur.fetchone() 20cur.execute("SELECT B FROM dex WHERE name = '%s'" % main.dname.get()) 21B = cur.fetchone() 22cur.execute("SELECT D FROM dex WHERE name = '%s'" % main.dname.get()) 23D = cur.fetchone() 24cur.execute("SELECT A FROM dex WHERE name = '%s'" % main.aname.get()) 25A = cur.fetchone() 26cur.execute("SELECT C FROM dex WHERE name = '%s'" % main.aname.get()) 27C = cur.fetchone() 28 29cur.execute("SELECT S FROM dex WHERE name = '%s'" % main.dname.get()) 30dS = cur.fetchone() 31cur.execute("SELECT mtype FROM moves WHERE name = '%s'" % main.mname.get()) 32mmtype = cur.fetchone() 33cur.execute("SELECT type FROM moves WHERE name = '%s'" % main.mname.get()) 34mtype = cur.fetchone()

Python

1#main.py 2import tkinter as tk 3import tkinter.ttk as ttk 4 5win = tk.Tk() 6win.title('ダメージ計算') 7win.geometry('600x600') 8 9atkframe = tk.Frame(win) 10atkframe.pack(pady = 5) 11 12nameframe = tk.Frame(atkframe) 13nameframe.pack(pady = 5) 14alabel = tk.Label(nameframe, text = "攻撃側 ") 15alabel.pack(side = 'left') 16aname = tk.Entry(nameframe, name = 'aname') 17aname.pack(side = 'left') 18aname.insert(index = 'insert', string = "インテレオン") 19 20lvframe = tk.Frame(atkframe) 21lvframe.pack(pady = 5) 22alvlabel = tk.Label(lvframe, text = "レベル ") 23alvlabel.pack(side = 'left') 24alventry = tk.Entry(lvframe) 25alventry.pack(side = 'left') 26alventry.insert(index = 'insert', string = '50') 27 28idframe = tk.Frame(atkframe) 29idframe.pack(pady = 5) 30aivlabel = tk.Label(idframe, text = "個体値 ") 31aivlabel.pack(side = 'left', ipadx = 5) 32aiventry = tk.Entry(idframe) 33aiventry.pack(side = 'left') 34aiventry.insert(index = 'insert', string = '31') 35 36aevlabel = tk.Label(idframe, text = "努力値 ") 37aevlabel.pack(side = 'left') 38aeventry = tk.Entry(idframe) 39aeventry.pack(side = 'left', ipadx = 5) 40aeventry.insert(index = 'insert', string = '252') 41 42natureframe = tk.Frame(atkframe) 43natureframe.pack(pady = 8) 44anaturelabel = tk.Label(natureframe, text = "性格 ") 45anaturelabel.pack(side = 'left') 46anaturecheck = ttk.Combobox(natureframe, state = 'readonly') 47anaturecheck['values'] = ("", "さみしがり", "いじっぱり", "やんちゃ", "ゆうかん", "ずぶとい", "わんぱく", "のうてんき", "のんき", "ひかえめ", "おっとり", "うっかりや", "れいせい", "おだやか", "おとなしい", "しんちょう", "なまいき", "おくびょう", "せっかち", "ようき", "むじゃき", "てれや", "がんばりや", "すなお", "きまぐれ", "まじめ") 48anaturecheck.current(2) 49anaturecheck.pack(side = 'left') 50 51moveframe = tk.Frame(atkframe) 52moveframe.pack() 53mlabel = tk.Label(moveframe, text = "わざ名 ") 54mlabel.pack() 55mname = tk.Entry(moveframe) 56mname.pack() 57mname.insert(index = 'insert', string = "アクアジェット") 58 59

Python

1#damagecalculation.py 2import tkinter as tk 3import tkinter.ttk as ttk 4import sqlite3 as sql 5 6import main 7import calculation 8 9calc = tk.Button(main.win, text = '結果', command = calculation.calc) 10calc.pack() 11 12main.win.mainloop()

試したこと

関連している部分の位置を変える、def calc()内に入れる等してみましたができませんでした。
以前にも同様の質問をしたため、それも参考にbuttonの実行している関数内に入れるのかと思ったのですが...

補足情報(FW/ツールのバージョンなど)

使用言語:Python 3.8.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

importは1回しか実行されません。2回目以降は無視して何も実行しません。
cursor.py などで直接処理を書いてあるのをすべて関数化し、calculation.pyからはそれらの関数を呼び出すようにすることで、毎回新しい値を取得できます。
関数やクラスやメソッドで必要な情報は、main処理から引数で渡してください。
引数が多くなるようなら、情報をまとめたオブジェクト(クラスのインスタンス)を渡すといいです。

import は関数の中に書かず、ファイルの先頭に書きましょう。
参考: https://pep8-ja.readthedocs.io/ja/latest/

import文 は常にファイルの先頭、つまり モジュールコメントや docstring の直後、そしてモジュールのグローバル変数や定数定義の前に置くようにします。

投稿2020/02/14 18:50

編集2020/02/14 19:40
shiracamus

総合スコア5406

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問