Python Forum
Class Struggle: Connecting a Method to an Outside Variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Struggle: Connecting a Method to an Outside Variable
#6
(Jan-23-2018, 06:52 AM)Gribouillis Wrote: I'm not sure it's a good idea to remove the explicit keywords when calling tkinter's methods, because the order of the arguments may be incorrect. Also you may need to keep a reference to the object you created in the init method, for example
class btn(object):
    def __init__(self, pos, txt, cmd, row, col, colspan, sticky):
        self.position = pos
        self.text = txt
        self.command = cmd
        self.row = row
        self.column = col
        self.columnspan = colspan
        self.sticky = sticky
 
        self.myVar = Button(self.position, text=self.text, command=self.cmd)
        self.myVar.grid(row=self.row, column=self.column, columnspan=self.columnspan, sticky=self.sticky)
Concerning myVar.bind('Button-1', myMethod), this call binds the event 'Button-1', which is the left mouse button event to the method myMethod for the tkinter widget myVar. Of course you can use this call only if there are a widget and a method named myVar and myMethod in the scope of the call.

The explicit order of the args is addressed via the grid args, isn't it? Am I to understand that version 1 is more correct than version 2?

(Jan-23-2018, 10:53 AM)Larz60+ Wrote: using myVar as a name for a widget is misleading.
the textvariable attribute should point to a tk.StringVar which you can preset using
that attribute and a tk.StringVar.
The StringVar can be set externally with string_var_name.set() (replace string_var_name with actual name)
It can be initally be set within the button definition
string_var_name = StringVar()
self.myVar = Button(self.position, text=self.text, textvariable=string_var_name, command=self.cmd)
Again, rename myVar to a meaningful widget name.
Also to be in conformance with PEP8, don't use camel case, use underscore instead
see: http://effbot.org/tkinterbook/variable.htm

Sorry for the edits, just got up

Going through PEP8 documentation and getting all those things right is a 2nd priority for the time being. It wouldn't make much sense sifting through that information if you basically don't know how to write code; sort of like learning grammar theory without knowing how to read/spell.
It is in the post, however, and I do understand what you're trying to convey. And I'm trying to adhere whilst unlearning/modifying what I know from ActionScript.

"myVar" doesn't belong. I think. It's there for lack of an alternative method (what I'm looking for), but we could call it whatever you would prefer as far as I'm concerned. "myString", "myTempWord", "Knudpovlsen" or even "Giraffe".
Reply


Messages In This Thread
RE: Class Struggle: Connecting a Method to an Outside Variable - by vulpesVelox - Jan-23-2018, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium big struggle Troop 2 1,760 Apr-25-2020, 10:47 AM
Last Post: Troop

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020