[Tutor] were do i put the random number genorator

Alan Gauld alan.gauld at btinternet.com
Fri May 11 09:29:19 CEST 2007


"Treloar, Nick" <Nick.Treloar at education.nsw.gov.au> wrote
> i am tyin to make a number guesing game were do
> i put the random genorator it the top of each child
> screen def

Frankly thats the least of your problems.
You need to consider how you are going to
pass references to the various widgets around,
which, if you don't use classes/objects, will mean
lots of global variables. For example your Nick() function
tries to access a widget called child1.box_txt but no
such widget exists.

There is a box_txt local variable inside child1 but that
variable is destroyed as soon as child1 exits.
You will need to pass that value out to a global
variable for Nick() - and any other event handler - to
see it.

The same applies to the Entry field. And the
same applies to the widgets created in child2()
Creating a random number is easily done in a
separate function which can be called from
anywhere in your program, you don't need to worry
about where it is located. But until you have
references to your widgets you won't be able to
display the random number or do much of anything
withb the windows you are creating.

Also child2 and child3 are identical except for the title,
so why not make them a single function with a title
parameter. Then you can just call the function with
the title text as an argument.

here is my code
from Tkinter import*
root = Tk()
root.title("main screen")
root.maxsize(width=350,height=200)
root.minsize(width=350,height=200)
root.resizable(width=YES,height=YES)

def child1():
        c1 = Toplevel(root)
        c1.guess_ent = Entry(c1, width = 35,)
        c1.guess_ent.grid(row = 14, column = 0)
        c1.box_txt = Text(c1, width = 35, height = 5, wrap = WORD)
        c1.box_txt.grid(row = 3, column = 0, columnspan=2)
        c1.title("easy")
        c1.geometry("200x200")
        Button(c1,text="clear", command = NIck).grid(row=1,column=0)
        Button(c1,text="new game",).grid(row=1,column=1)
def NIck():
    child1.box_txt.delete(0.0, END)

def child2():
    c2 = Toplevel(root)
    box_txt = Text(c2, width = 35, height = 5, wrap = WORD)
    box_txt.grid(row = 3, column = 0, columnspan=2,sticky = W)
    c2.title("medium")
    c2.geometry("200x200")
def child3():
    c3 = Toplevel(root)
    box_txt = Text(c3, width = 35, height = 5, wrap = WORD)
    box_txt.grid(row = 3, column = 0, columnspan=2,sticky = W)
    c3.title("hard")
    c3.geometry("200x200")

Label(root,text = "choose which game you would like to 
play").grid(row=0,column=0,columnspan=2)
Button(root,text="easy",command=child1).grid(row=1,column=0)
Button(root,text="medium",command=child2).grid(row=1,column=1)
Button(root,text="hard",command=child3).grid(row=1,column=3)


This message is intended for the addressee named and may contain 
privileged information or confidential information or both. If you are 
not the intended recipient please delete it and notify the sender.



--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list