私の問題は私のtkinter
アプリケーションです。私は、アプリケーションの一部を折りたたむためにボタンを使用していますが、左の各ボタンをクリックすると、折りたたみボタンを7回押して、最初に作成されたアプリケーションの右側のすべてのインスタンスを取り除かなければなりませんcreateArea
メンバ関数内にあります。同じクラスの異なるインスタンス内からクラスメンバー関数にアクセスするにはどうすればよいですか? Python
クラスの各インスタンスをdictionary
という名前の辞書に保存しました。すべてのインスタンスで、アプリケーションの右側をすべて破棄する機能が必要です(destroyRight
)。
辞書に格納されている各クラスインスタンスの機能にアクセスする方法がわかりません。また、クラスのインスタンスを取得して他のすべてのインスタンスと通信できるかどうかもわかりません。
助けていただければ幸いです。
from tkinter import*
root = Tk()
class Buttons:
def __init__(self,master,imperialText,metricText,metricVal):
self.imperialText,self.metricText,self.metricVal,self.master = imperialText,metricText,metricVal,master
self.displayedText = (self.imperialText +'-'+ self.metricText)
self.button = Button(self.master,text= self.displayedText,command = self.createArea)
self.button.config(height= 3,width=30)
self.button.grid(column = 0)
self.rightCreated = False
self.rightButtons = []
def createArea(self):
self.rightCreated = True
self.entryBox = Entry(self.master)
self.entryBox.bind('<Return>',self.calc)
self.entryBox.grid(column = 1,row = 1)
self.label = Label(self.master,text = 'Enter '+self.imperialText)
self.label.grid(column = 1,row = 0)
self.backButton = Button(self.master,text = '<<Collapse', command = self.destroyRight)
self.backButton.grid(column = 1, row = 6)
print('happen')
self.rightButtons.extend([self.entryBox,self.label,self.backButton])
def destroyRight(self):
collapseAll()
print(self.rightButtons)
for i in self.rightButtons:
i.destroy()
def calc(self):
print('hi')
def collapseAll():
for i in dictionary:
dictionary[i].destroyRight()
dictionary = {'B1':None,'B2':None,'B3':None,'B4':None,'B5':None,'B6':None,'B7':None}
ImperialText = ['inches','miles','foot','yards','gallons','pounds','ounces']
MetricText = ['centimetres','kilometres','metres','metres','litres','kilograms','grams']
metricVal = [2.54,1.6093,0.3048,0.9144,4.546,0.454,0.454]
num = 0
for i in dictionary:
dictionary[i] =
Buttons(root,ImperialText[num],MetricText[num],metricVal[num])
num += 1
if num == 6:
print(i)
root.mainloop()
これをクラスの中に入れ、他のクラスインスタンスの関数を呼び出してもいいですか? Answer btw –
いいえ、特定のインスタンスのメソッドを呼び出す場合は、そのメソッドを明示的に呼び出す必要があります。たとえループ内にあっても 'for dictionary in i [i] .calc () ' – 0TTT0
ああ、私の質問に答えてくれてありがとう。私は明日の朝にこれを試してみる。 –