[Tutor] why can use a widget assigned to a variable or just use it on it's own?

Timo timomlists at gmail.com
Mon Jul 2 03:24:44 EDT 2018


Op 02-07-18 om 03:54 schreef Chris Roy-Smith:
> Hi,
>
> I'm trying to understand working with objects.
>
> If I have grasped things correctly a widget is an object. So why can I 
> assign the widget, or use it stand alone? See sample code below
>
> =====================
>
> #!/usr/bin/python3
> from tkinter import *
> main=Tk()
>
> # as I understand it this will create an instance of the button widget 
> called b1
> b1=Button(main, text='instantce', command= lambda b='goodbye' : 
> print(b)).grid(row=1, column=0)
>
>
> # but here I haven't made an instance, but all seems well
> Button(main, text='test1', command=lambda a='hello' 
> :print(a)).grid(row=0, column=0)
>

You still create an instance, but don't assign it to a variable. See 
following example:

class Foo:
     def __init__(self):
         print("Instance created")

f = Foo()  # prints "Instance created"
Foo()  # print "Instance created"

Two instances are created, just that the second one isn't accessible.

Timo

> main.mainloop()
>
>
> =======================
>
> any explanation gratefully recieved
>
> Regards, Chris ROy-Smith
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list