TKinter is driving me crazy!

Jp Calderone exarkun at intarweb.us
Mon Sep 1 22:42:23 CEST 2003


On Mon, Sep 01, 2003 at 03:46:41PM -0400, tjland at iserv.net wrote:
> Okay now im pretty sure this is really easy and pretty stupid but i need
> to create this. To make it simple ill use another program as an example. A
> guessing game that has an input field and button and an output field that
> can display w/e. I know this is really easy in c++ but it needs ot be done
> in python. Can anyone start me off here or give me a skeleton structure on
> this is supposed to be arranged. Thanx in advance.
> 

  Just off the top of my head...

    import Tkinter as tk

    class GuessWidget(tk.Frame):
        def __init__(self, value, *args, **kw):
            tk.Frame.__init__(self, *args, **kw)
            input = tk.Frame(self)
            tk.Label(input, text="Guess").pack(anchor=tk.W)
            self.input = tk.Entry(input)
            self.input.bind('<Return>', self.got_input)
            self.input.pack(anchor=tk.W)
            input.pack(anchor=tk.N)
            self.output = tk.Label(self)
            self.output.pack(anchor=tk.N)
            self.pack()
            self.value = value

        def got_input(self, event):
            v = int(self.input.get())
            if v < self.value:
                self.output.config(text="Too low!")
            elif v > self.value:
                self.output.config(text="Too high!")
            else:
                self.output.config(text="You got it!")

    if __name__ == '__main__':
        GuessWidget(random.randrange(100)).mainloop()

  Jp

--

        "I quite agree with you," said the Duchess; "and the moral of
that is -- Be what you would seem to be' -- or, if you'd like it put
more simply -- Never imagine yourself not to be otherwise than what it
might appear to others that what you were or might have been was not 
otherwise than what you had been would have appeared to them to be
otherwise.'"       -- Lewis Carrol, "Alice in Wonderland"





More information about the Python-list mailing list