Re: [Tutor] Buttons
On 27/05/13 15:17, Jack Little wrote: Is there a way to make buttons a little like a raw_input in the context when the user presses the button, the program performs a function? Yes, but its a lot more complicated than raw_input. See the GUI topic of my tutorial for examples. Also check out the EasyGUI package for an easy way to add GUI style dialog boxes to an otherwise console based app. (see my "Talking to the User" topic in my V3 tutorial for more on EasyGUI) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Buttons automatically executing
On Wed, 15 Jun 2005, Phillip Hart wrote: > Thanks for taking the time to read this. > > Excuse me if this falls under the boundaries of a "newbie" question, as > I am a self-taught programmer. Hi Phillip, It's a "newbie" question. But that's perfectly ok. *grin* > While using Tkinter to create buttons, something unexpected happens when > the button commands call functions. Namely, when I run the following, it > immediately executes the print statements without the button ever being > pressed. Additionally, pressing the button once the Tk window is up will > not activate the button. The issue is that there's a difference between calling a function, and getting a function value. Let's go through an example to get started: For example, let's say that we do have that 'location' function defined: ### >>> def location(x, y): ... print "I am in row", x, "and column", y ... ### A function value is what we get when we don't use parentheses: ### >>> location ### And this thing can be handled just like any other value in Python: we can pass it around and put it in lists: ### >>> somelist = [location, location, location] >>> somelist [, , ] ### One neat thing that you already know about functions is that they can be called. You're used to doing something like this: ### >>> location(3, 4) I am in row 3 and column 4 ### but we can also do something like this: ### >>> somelist[0] >>> somelist[0](2, 3) I am in row 2 and column 3 ### That is, 'somelist[0]' is a function value, and when we use parens next to it '(2, 3)', then we fire off that function value with those two arguments. Does this make sense so far? When we're constructing a button: b1=Button(can,bg="black",image=bimage,command=location(0,0)) we should be careful to pass a function value as the 'command': if we use parens prematurely, what ends up happening is that command gets set to the return value of calling 'location(0, 0)'. It looks we want to set 'command' to a function value that, when called, does a 'location(0,0)'. We can do that: ### def reset(): location(0, 0) b1 = Button(can,bg="black",image=bimage,command=reset) ### 'reset' is a function that, when called, does location(0, 0). Please feel free to ask more questions about this. Good luck to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Buttons automatically executing
On Wed, 15 Jun 2005 14:31:55 -0500 Phillip Hart <[EMAIL PROTECTED]> wrote: Hi Phillip, > #!usr/bin/python > > from Tkinter import * > > def location(x,y): > print "I am in row ",x," and column ",y > > > root=Tk() > root.geometry('300x300') > can=Canvas(root,width=250, height=250) > > bimage=PhotoImage(file='sq1.gif') > > b1=Button(can,bg="black",image=bimage,command=location(0,0)).grid(row=0,column=0) That's an error which frequently happens to Tkinter beginners. When you assign the button's command like this the command is executed immediately after button creation, the correct usage is: b1 = Button(can, command=location) without the parentheses; however if you need to pass arguments to the callback, you can use a lambda expression like this: b1 = Button(can, command = lambda x=0, y=0 : location(x, y)) Another problem in this line that doesn't matter here, but might cause confusion later on is the use of grid() ; note that grid() returns None, so actually you set your variable b1 to None which is probably not what you intended; if you need to keep a reference to the Button object you need to split the above into two statements: b1 = Button(can, ) b1.grid() If you don't need a reference you can simply do: Button(can,).grid(row=0,column=0) without creating a (quite useless) variable. I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] buttons, scrollbar, tkinter
Quoting Ron Alvarado <[EMAIL PROTECTED]>: > Is there any way to change this to put in a column of buttons and it > will scroll like it does now. I've tried but when I put in the buttons the > scrollbar just grows with all the buttons and won't do anything. I'm not exactly sure what you're after here ... You can't put buttons into a Listbox. A Listbox will only take strings. You can attach a scrollbar to a Frame and pack a bunch of buttons into that. But I find the easiest solution to making scrolling things is to get Pmw: http://pmw.sourceforge.net/ . A Pmw.ScrolledFrame will probably do what you want with very little effort on your part. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor