python, tkinter, command is executed when program is run not when button clicked
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
when i run this program the line is draw in the canvas as soon as the program is run, not when the button is clicked. Also when i click the button nothing happens,
First of all your indentation makes it difficult to see which commands belongs to which methods. I am going to assume that the indentation in your program is mostly correct or it wouldnt run. Without seeing the correct indentation you have used. I would say that the command to draw the line isn't indented propely.
Ok I see your problem here and you need to regoranise you code.
First of all you have a function inside a function. This isn't a good thing to do in python unless you need a closure which you don't. You could do any of the following to improve it:
a) You could move hello_world outside the Application class
b) make hello_world a method of the Application class
c) Create a new class for the Canvas and make hello_world a method of that
d) Get rid of hello_world entirely and use lambda instead
Any of which would be better and which you chose depends on how and where you want the function to be called.
Your going to have to do this this anyway because what you have told python here:
is create a new Button called self.newbutn with the parameter of self, the value of the text "Draw Line" and the value of command to be whatever hello_world(self.cnvs) evaluates as which is None. Which is not what you wanted. What you wanted to say is:
Note you cannot pass self.cnvs to the command because what You are passing is a function object to be called later. Hence why you need to reorganise the code a bit.
if you really need to. There are 2 things you can do
1 create a function class and pass the attributes to the function:
In python a function is basically just an object with the method __call__ note the double underscore signifying its a special method. When a function gets called its __call__ which will get called so you could write something like:
you can then create a Command object passing it the functions and any arguments and keyword arguments ready to be called later and it will behave as any other function
2. Using a closure, in python nested scopes are allowed. Hence why you can write hello_world inside of createWidgets using this technique you can write a function which will write a function. Particulary useful if the inner function functionality remains the same but what it operates on doesnt.
Code:
def factoryMethod(self, *args, **keywds):
def myfunc(): pass #myfunc can see args and keywds
return myfunc
you can call factoryMethod and myfunc functioned will be returned operating on the parameters passed initally to factoryMethod
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.