Greetings:

I am having trouble understanding how buttons launch actions such as, for example, loading a new (2nd) canvas.

Suppose I have an application, a game, that will respond at some point to user imput via buttons: I have two buttons named "Yes" and "No"; now if the user selects "No" nothing happens and the game is in the same state it was before the buttons appeared for user input. But, if the user chooses "Yes" a function should be launched, say a Python RNG; this button will force the program to drop into the area of code holding the RNG, the RNG will run and return a value. I have the RNG and it works well, but I am not sure how I will code the buttons to call upon the RNG.

I am new to using GUI interfaces and it seems a bit abstract and disconnected at this point. I am learning much by researching this (and all related questions) but I need help with the theory of button use, how it is used to fire code, and and how it is best to code it. I just need to know where to look and I need to understand what I find.

Thank-you in advance,
reRanger

I am a little confused, hope this will help ...

# create a row of colorful buttons and assign some action to each
from Tkinter import *

def action1():
    label1.config(text='You pressed button1')
    
def action2():
    label1.config(text='You pressed button2')
    
def rng():
    label1.config(text='Now you could be running an RNG!')

form1 = Tk()
form1.title('Press a button')

# assign desired function object to command of each button
button1 = Button(form1, text='button1', fg='blue', command=action1)
button2 = Button(form1, text='button2', fg='brown', command=action2)
rng_button = Button(form1, text='run RNG', bg='green', command=rng)
quit_button = Button(form1, text='<< quit >>', fg='red', command=form1.destroy)
label1 = Label(form1, fg='red', bg='yellow')

# use a grid to place widgets on the form
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
rng_button.grid(row=0, column=2)
quit_button.grid(row=0, column=3)
label1.grid(row=1, column=0, columnspan=4)

form1.mainloop()
commented: Always very helpful and concise. +1

Hi:

Thank-you for that code-- interesting.

What I do not understand though is how does that green "RNG" button (when pressed) activate code, say, a RNG? Is this bound somehow to

rng_button = Button(form1, text='run RNG', bg='green', command=rng)

by the command=rng? Is rng in this case a keyword that would actually activate a RNG? I have a nice snip of code that I will probably use to produce a random effect-- the question is, how do I bind this button ("RNG") to the RNG code I possess?

Thank-you for all you help ;) I'm learning more each day here...

reRanger

Its me, Ene,
note that each button ends up executing a function defined in the program. The button uses the command=function statement. The function is activated via its object (ie. just function rng), and not the usual function call (ie. would be rng() ).

Also note that rng is not a keyword, but just a name vegaseat picked for the function he defined earlier. So if you want an RNG going, put it into the function rng() (or whatever you want to name it), then activate it via command=rng.

commented: Always helpful and nice ;) +1

Hello:

Thank-you for your reply.

Let me see if I understand this:

  • One presses the button ( an object) and "command=rng"is activated via:"rng_button = Button(form1, text='run RNG', bg='green', command=rng)"
  • "command=rng" refers back to "def rng():" which simply runs the text message 'Now you could be running an RNG!' There instead could be a full function here, for example, a RNG? Coded as something like: def myRNG(rng): ?

Thanks for all your assistance and comments-- it really helps me.


reRanger

You got it correctly! I guess Ene explains things a little better than me. Once you know it, it is simple. Things get a little bit more complicated if you have to pass arguments to this particular function object. Then you have to wrap the command. If you ever have to do that, just ask.

Actually Bumsfeld has written some code about this very subject in the "Starting Python" thread, see: http://www.daniweb.com/techtalkforums/post224920-61.html

Hallo: The below code has been adjusted just abit with the intention of adding the "shuffle" def. It does not work very well and returns the following error-- the error code "takes at least 2 arguments (1 given)" is very confusing to me (**also,I am using the drPython IDE):

C:/Python24/pythonw.exe -u  "C:/Documents and Settings/RockStar/Desktop/Python/drP/drP/drpython-161/RNG_button.py"
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
TypeError: shuffle() takes at least 2 arguments (1 given)

---------------------------------------------Source Code

# create a row of colorful buttons and assign some action to each
from Tkinter import *
from random  import*

def action1():
    label1.config(text='You pressed button1')
    
def action2():
    label1.config(text='You pressed button2')
    
#def dice():
    #label1.config(text='Now you could be running an RNG!')

def shuffle(x):
    for i in xrange(len(x)-1, 0, -1):
    #pick an element in x[:i+1] with which to exchange x[i]
            j = int(random.random() * (i+1))
            x[i], x[j] = x[j], x[i]
    
    
    
form1 = Tk()
form1.title('Press a button')

# assign desired function object to command of each button
button1 = Button(form1, text='button1', fg='blue', command=action1)
button2 = Button(form1, text='button2', fg='brown', command=action2)
rng_button = Button(form1, text='run RNG', bg='blue', command=shuffle)
quit_button = Button(form1, text='<< quit >>', fg='red', command=form1.destroy)
label1 = Label(form1, fg='red', bg='yellow')

# use a grid to place widgets on the form
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
rng_button.grid(row=0, column=2)
quit_button.grid(row=0, column=3)
label1.grid(row=1, column=0, columnspan=4)

form1.mainloop()

Admittably, I am having some issues putting some of these concepts together:o -- I feel I am very close, though :)!

Thank-you in advance,
reRanger

Now you are getting into passing an argument, easy with lambda:

# create a row of colorful buttons and assign some action to each
from Tkinter import *
from random  import*
import random

def action1():
    label1.config(text='You pressed button1')
    
def action2():
    label1.config(text='You pressed button2')
"""
def dice():
    label1.config(text='Now you could be running an RNG!')
"""
def shuffle(x):
    # random.shuffle(x) would be simpler
    for i in xrange(len(x)-1, 0, -1):
        #pick an element in x[:i+1] with which to exchange x[i]
        j = int(random.random() * (i+1))
        x[i], x[j] = x[j], x[i]
    #print x  # test
    label1.config(text=x)
    
    
form1 = Tk()
form1.title('Press a button')

# assign desired function object to command of each button
button1 = Button(form1, text='button1', fg='blue', command=action1)
button2 = Button(form1, text='button2', fg='brown', command=action2)
# somehow you have to supply object x!!!!!!!!
# I assumed a list of integers
x = [1,2,3,4,5,6]
# since you are passing an argument to shuffle(), use lambda as the wrapper
rng_button = Button(form1, text='random shuffle', fg='white', bg='blue', command=(lambda: shuffle(x)) )
quit_button = Button(form1, text='<< quit >>', fg='red', command=form1.destroy)
label1 = Label(form1, fg='red', bg='yellow')

# use a grid to place widgets on the form
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
rng_button.grid(row=0, column=2)
quit_button.grid(row=0, column=3)
label1.grid(row=1, column=0, columnspan=4)

form1.mainloop()

Thank-you ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.