On 15/01/16 08:34, Alan Gauld wrote: > maybe you can get the mouse positions and put them in order to draw a > line,etc.
Yes, that's a valid approach to building a multi-segment line. Normally you store the points in a list somewhere. > I did not want to use global variable because I am not used to this > function in python or any lambda function because it's quite hard. You can avoid lambdas but I think you need to use globals to get this to work. You are already using some global variables in your code, you just need to identify which ones you want to change in your functions. That's simply done using the global keyword, so your function would look like: def some_function(event): global widget_name etc... widget_name = new_value etc... It is really quite easy and allows you to store information from inside a function so that other functions can see it. The alternative strategy involves creating classes and objects but that is possibly too advanced for you just now. The problem with your existing code is that you are creating the widgets inside functions. But when the function ends you lose all references to those widgets so you cannot modify them or even read them in any way. (Although the variables all currently refer to None because you use pack() in the same line as you create them.) >> def mouse_position(): >> show_event=app.winfo_pointerxy() >> (X,Y)=show_event >> if '<Motion>'!=show_event : >> show_event=app.winfo_pointerxy() >> (X,Y)=show_event >> print("Current mouse are on: X:{0} Y:{1}".format(X,Y)) >> label2=Label(frame1,text=str(show_event), > relief=GROOVE).pack(side=RIGHT) > > And you are right that I have to call the same code to find the > mouse-position again and again. Actually you don't really need a function there, I just realized you can simplify it by just rewriting it as a single line: X,Y = app.winfo_pointerxy() > Then again I don't know what got me but I thought that I could write a > function for mouse pressed which will print(Right Click has been > pressed.) and take a variable to store the mouse position. Now I think I > can modify the code to use separetely to find the coordinates but I > don't know how to do that. Because in each right click the initialpos > and finalpos of the mouse will change and they would have the same > value. Should not they?? Sorry, I'm not sure what you mean there. Can you try explaining again please? > If you had tested the code you might have notice that each time I am > pressing the show button the command keep printing or making a label to > the right. How do I keep only a single label and delele the old label. You need to create a global variable and then update that label. > So any idea how should I store the initial and final position e.g. > inside a function or as global value.?? Anything that you do in one function that you want to be visible in another function (or even the same one called a second time) will need to be stored in a global variable. Variables inside functions get deleted as soon as the function exits so you cannot refer to them again. Each time the function gets called it creates a new set of variables it does not use the same ones as the last time. > about mouse position functions? Should I reduce any of them? Is it a > problem to put labels inside them? Its OK to create labels inside but they will always be new labels unless you use global variables. [Note 1: Strictly speaking there are some other ways to maintain data between function calls but I'm pretty sure they are too esoteric for your purposes. Globals or classes are by far the simplest solutions Note 2: Technically the widgets you are creating do have references outside the function, otherwise they would be destroyed, but to reach them involves traversing the app object's containment tree which is relatively complicated and error prone ] -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor