Tkinterの行を更新できません。Tkinterの行を変数に設定する
行を通常の変数に設定すると、更新されません。これは最初のスクリプトに示されています。 テキストと同様に行をIntVar型に設定すると、データ型が拒否されます。これは2番目のスクリプトに示されています。
2注意事項: スクリプト1でカウンタを見ると、上がってもうまくいきますが、適用されません。 あなたの代わりにself.activeRowのself.activeRow.get()を使用している場合は、それが効果的にスクリプトで1
を示したものと同じ結果を通常の変数にそれを向けるだろうスクリプト1
from tkinter import *
class Example(Frame):
def move(self):
self.activeRow += 1
print(self.activeRow)
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=0)
self.columnconfigure(1, pad=0)
self.columnconfigure(2, pad=0)
self.rowconfigure(0, pad=0)
self.rowconfigure(1, pad=0)
self.rowconfigure(2, pad=0)
Label(self, text= 'row 0').grid(row=0, column=0)
Label(self, text= 'row 1').grid(row=1, column=0)
Label(self, text= 'row 2').grid(row=2, column=0)
#regular variable
self.activeRow = 0
b = Button(self, text="normal variable {0}".format(self.activeRow), command=self.move)
b.grid(row=self.activeRow, column=1)
self.pack()
def main():
root = Tk()
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
スクリプト2
from tkinter import *
class Example(Frame):
def move(self):
self.activeRow.set(self.activeRow.get() + 1)
print(self.activeRow.get())
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.columnconfigure(0, pad=0)
self.columnconfigure(1, pad=0)
self.columnconfigure(2, pad=0)
self.rowconfigure(0, pad=0)
self.rowconfigure(1, pad=0)
self.rowconfigure(2, pad=0)
Label(self, text= 'row 0').grid(row=0, column=0)
Label(self, text= 'row 1').grid(row=1, column=0)
Label(self, text= 'row 2').grid(row=2, column=0)
#Tkinter IntVar
self.activeRow = IntVar()
self.activeRow.set(0)
b = Button(self, text="IntVar", command=self.move)
b.grid(row=self.activeRow, column=1)
self.pack()
より完全なコード例を提供できますか? 'row = self.activeRow'は' self'が 'activeRow'に設定された値を持っていれば動作します。 – cfedermann
さて、私はちょうど詳細でそれを書き直しました。 私の書式設定/スタイルがオフの場合、plsは私が6週間前にプログラムする方法を学び始めたので私に知らせてくれます。 – Talisin
ああ、ちょうどあなたがそれを再読み込みするのを救うために、問題は: もしself.activeRowが普通の変数であれば、それは更新されませんし、IntVarならintではないので受け入れられません。だから私はそれが行を変更することができますか? – Talisin