type instance

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
algol60
User
Beiträge: 3
Registriert: Dienstag 27. Juni 2017, 07:03

Hallo

komme mit einer instance Uebergabe nicht weiter, deshalb habe ich mir hier im Forum angeneldet.
Ich will mir mit Tkinter eine GUI erstellen
Vielleicht kann mir einer helfen.
Folgendes Problem:

Hauptprogramm:

Code: Alles auswählen

if __name__ == 'main':
      " code ... code"

#----------------------------------
#erzeugen eines labels
#----------------------------------

    root = Tk()

    techId = ModuleBar(root,.........)

    label1 = Label(root, trext= "labeltext")
    label1.grid(row= 1 ,........)

    techId.MyLabel(label1)

#-------------------------------------------
# class def Modulebar
#-------------------------------------------
class ModuleBar(Frame):
    def __init__(self, .......)
        self.var = IntVar()
        "code ..code"

    def MyLabel(self,var):
        print type(var)
#----------------------------------------------------------
#  var type ist hier <type 'instance'>
#----------------------------------------------------------
        self.name = var

    def MyAction(self):
#---------------------------------------------------------
# routine soll den text des labels aendern
#---------------------------------------------------------
        labelMod =- self.name
        labelMode.config(text="NewLabelText"  

Irgendwie muss ich die Variable in __init__ als instance definieren.
Hier scheint mein Problem zu liegen, ich bekomme folgende Fehlermeldung

AttributeError: object has no attribute 'config'

Was muss ich machen das ich in der Action routine das label aender kann.

Gruss
Zuletzt geändert von Anonymous am Dienstag 27. Juni 2017, 09:48, insgesamt 1-mal geändert.
Grund: Quelltext in Python-Codebox-Tags gesetzt.
Benutzeravatar
Kebap
User
Beiträge: 686
Registriert: Dienstag 15. November 2011, 14:20
Wohnort: Dortmund

Hallo und willkommen bei Python! :)

Wenn ich deinen Code ausführe, sehe ich ganz andere Fehlermeldungen. Kannst du bitte ein *lauffähiges* kleines Beispiel zeigen, das dein Problem verdeutlicht? Danke!
MorgenGrauen: 1 Welt, 8 Rassen, 13 Gilden, >250 Abenteuer, >5000 Waffen & Rüstungen,
>7000 NPC, >16000 Räume, >200 freiwillige Programmierer, nur Text, viel Spaß, seit 1992.
algol60
User
Beiträge: 3
Registriert: Dienstag 27. Juni 2017, 07:03

Hallo habe meine Code etwas modifiziert, hoffe das es geht.

es sind 2 files

1 Hauptprogamm

Code: Alles auswählen

#from PrimlibDef import *
from PrimlibGui import *

if __name__ == '__main__':

	def okButton(): 
    		print "selected "


#	Primlib = PrimlibDef("xh035")

	libCode = "1000"
#	libCode    = Primlib.getlibCode()
#	moduleName = Primlib.getModuleName() 
#	moduleDef  = Primlib.getModuleDef() 
#	buttonCtrl = Primlib.getModuleButtonCtrl() 

	root = Tk()
	

        moduleDef = [[[11, 'aaa'], 
	              [12, 'aab'], 
		      [13, 'aac'], 
		      [14, 'aad']], 
		     [[20, 'aba'], 
		      [21, 'abb']], 
		     [[30, 'aca'], 
		      [31, 'acb'], 
		      [32, 'acc'], 
		      [33, 'acd'], 
		      [34, 'ace']], 
		     [[40, 'ada'], 
		      [41, 'adb']]]

	row = 0
	for modTmp in moduleDef:
		bList = []
		for tmp in modTmp:
			tmpList = []
			tmpList.append(tmp[1])
			tmpList.append(tmp[0])
			bList.append(tmpList)
    		techId = ModuleBar(root, bList, side=TOP, anchor=NW, libCode=libCode,)
    		techId.config(relief=RIDGE,  bd=2)

   		techId.grid(row=row , column= 0, padx='5', pady='5', sticky='ew')
		row = row + 1

	textLibCode = "libCode:  " + libCode

	labelLibCode = Label(root,text= textLibCode)
	labelLibCode.grid(row=row+1 , column= 0, padx='5', pady='5', sticky='ew')

#	labelLibCode.config(text= "aaa")

	techId.changeLabel(labelLibCode)

	quit = Quitter(root)
	quit.grid(row=row + 2 , column= 1, padx='5', pady='5', sticky='ew')

	butt= Button(root, text='O.K.', command=okButton)
	butt.grid(row=row + 2 , column= 0, padx='5', pady='5', sticky='ew')

	root.mainloop()
Gui class definition

Code: Alles auswählen

from Tkinter import *

import tkMessageBox

class ModuleBar(Frame):
    name = None

    def __init__(self, parent=None, picks=[], side=LEFT, anchor=W, libCode= ""):
        Frame.__init__(self, parent)
        self.var = IntVar()
	 
	self.var.set(picks[0][1])
	self.libCode = libCode

	for txt, val  in picks:
            rad = Radiobutton(self, text=txt, value=val, variable=self.var, command=self.butttonPressed)
            rad.pack(side=side, anchor=anchor, expand=YES)

    def state(self):
        return self.var.get()

	
    def butttonPressed(self):
	code = self.var.get()
	labelLibCode = self.name

	print type(labelLibCode)
	
	labelLibCode.config(text= "bbb")

	print self.libCode


    def changeLabel (self,var):
	print var
	self.name = var
	print type(self.name)

class Quitter(Frame):                      
    def __init__(self, parent=None):         
        Frame.__init__(self, parent)
        widget = Button(self, text='Cancel', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = tkMessageBox.askokcancel('Title', "Really quit?")
        if ans:
	    Frame.quit(self)
Ziel ist das die action routine der Radiobutton jeweils den label text aendert.

Danke erstmal
Zuletzt geändert von Anonymous am Dienstag 27. Juni 2017, 09:50, insgesamt 1-mal geändert.
Grund: Quelltext in Python-Codebox-Tags gesetzt.
algol60
User
Beiträge: 3
Registriert: Dienstag 27. Juni 2017, 07:03

Hallo

nach etwas nachdenken habe ich meinen Fehler gefunden,
der erste war das ich die ID im Hauptprogramm mehrfach vergeben habe, und ausserdem musste ich mir noch eine locale Variable
in meiner class defineren muss.

hier die Aenderung

Code: Alles auswählen

#from PrimlibDef import *
from PrimlibGui import *

if __name__ == '__main__':

	def okButton(): 
    		print "selected "


#	Primlib = PrimlibDef("xh035")

	libCode = "1000"
#	libCode    = Primlib.getlibCode()
#	moduleName = Primlib.getModuleName() 
#	moduleDef  = Primlib.getModuleDef() 
#	buttonCtrl = Primlib.getModuleButtonCtrl() 

	root = Tk()
	

        moduleDef = [[[11, 'aaa'], 
	              [12, 'aab'], 
		      [13, 'aac'], 
		      [14, 'aad']], 
		     [[20, 'aba'], 
		      [21, 'abb']], 
		     [[30, 'aca'], 
		      [31, 'acb'], 
		      [32, 'acc'], 
		      [33, 'acd'], 
		      [34, 'ace']], 
		     [[40, 'ada'], 
		      [41, 'adb']]]

	row = 0
	for modTmp in moduleDef:
		bList = []
		for tmp in modTmp:
			tmpList = []
			tmpList.append(tmp[1])
			tmpList.append(tmp[0])
			bList.append(tmpList)
    		techId = ModuleBar(root, bList, side=TOP, anchor=NW, libCode=libCode,)
    		techId.config(relief=RIDGE,  bd=2)

   		techId.grid(row=row , column= 0, padx='5', pady='5', sticky='ew')
		row = row + 1

	textLibCode = "libCode:  " + libCode

	labelLibCode = Label(root,text= textLibCode)
	labelLibCode.grid(row=row+1 , column= 0, padx='5', pady='5', sticky='ew')

#	labelLibCode.config(text= "aaa")

	techId.setLabel(labelLibCode)

	quit = Quitter(root)
	quit.grid(row=row + 2 , column= 1, padx='5', pady='5', sticky='ew')

	butt= Button(root, text='O.K.', command=okButton)
	butt.grid(row=row + 2 , column= 0, padx='5', pady='5', sticky='ew')

	root.mainloop()

Code: Alles auswählen

from Tkinter import *

import tkMessageBox

class ModuleBar(Frame):
    label = None

    def __init__(self, parent=None, picks=[], side=LEFT, anchor=W, libCode= ""):
        Frame.__init__(self, parent)
        self.var = IntVar()
	 
	self.var.set(picks[0][1])
	self.libCode = libCode

	for txt, val  in picks:
            rad = Radiobutton(self, text=txt, value=val, variable=self.var, command=self.butttonPressed)
            rad.pack(side=side, anchor=anchor, expand=YES)

    def state(self):
        return self.var.get()

	
    def butttonPressed(self):
	print type(ModuleBar.label)
	code = self.var.get()
	print code
	if isinstance(ModuleBar.label,Label):
		labelLibCode = ModuleBar.label.config(text="%s" % code)
	else:
		print type(ModuleBar.label)


    def setLabel (self,var):

	ModuleBar.label = var

	print type(ModuleBar.label)

class Quitter(Frame):                      
    def __init__(self, parent=None):         
        Frame.__init__(self, parent)
        widget = Button(self, text='Cancel', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = tkMessageBox.askokcancel('Title', "Really quit?")
        if ans:
	    Frame.quit(self)
Danke erstmal
Zuletzt geändert von Anonymous am Dienstag 27. Juni 2017, 09:54, insgesamt 1-mal geändert.
Grund: Quelltext in Python-Codebox-Tags gesetzt.
Antworten