G'day,

Back again, this time with three problems. The code below contains a white entry box that I'm trying to get rid of. The main reason I'm trying to get rid of it is that the game I'm working on has a scrollable GUI, and with 50 or so frames would look kinda silly if it had a "white box" for entry on every frame. The Introduction to Tkinter ebook has everything about how to put in an entry box but nothing about NOT having an entry box something like this:

Enter your number: 4

The "4" itself would be put in by keyboard so that it doesn't have any "Entry box" at all, it just goes straight on the GUI as is and looks like it does above. Is this possible to do?:-/

Secondly, how does one code to play a new game i.e. the same game again in a "root.mainloop()" program without having to exit the game and re-enter it? Again the documentation is sparse on this matter.:icon_cry:

Lastly, how do you add an endless vertical scrollable bar that can be scrolled the normal way and also by the middle mouse wheel to all this?:confused:

Anyway, here's the code (I borrowed and changed it a bit) from Mr. Vegaseat:

# a simple Tkinter number guessing game
 
from Tkinter import *
import random
root = Tk()
root.geometry("400x300+30+30")
words1=("LOWER")
words2=("HIGHER") 
def click():
    # get the number in the entry area
    num = int(enter1.get())
    # check it out
    if num > rn:
        label3['text'] = words1
    elif num < rn:
        label3['text'] = words2
    else:
        label3['text'] = 'Guessed correctly!'
 
 
# pick a random integer from 1 to 10
rn = random.randrange(1, 11)
 
# let the user know what is going on
label1 = Label(root, text="Guess a number between 1 and 10")
label1.place(x=360, y= 20, width=200, height=15)
 
# prompt the user
label2 = Label(root, text="Enter your number: ")
label2.place(x=0, y= 10, width=200, height=20)
 
# this your input area
enter1 = Entry(root, width=82)
enter1.place(x=150, y= 10, width=200, height=20)
enter1.focus()
 
# this is your mouse action button, right-click it to execute command
button1 = Button(root, text=" Enter", command=click)
button1.place(x=600, y= 10, width=50, height=20)
 
# the result displays here
label3 = Label(root, text="", bg='green')
label3.place(x=0, y= 30, width=200, height=20)
 
# start the mouse/keyboard event loop
root.mainloop()

Thanks kindly for all of your help so far it's been greatly appreciated.:icon_biggrin:

fredzik.

Recommended Answers

All 6 Replies

I am not quite sure what you are trying to do, but your widget placements are off the map.

It looks to me like you are trying to make a console type game with a GUI. In that case I would recommend to use the text widget and scroll that.

To loop your game, you put the core of your game into a while loop and used two buttons. One button to continue and refresh the random number, and the other button to exit the game.

This comes pretty close to eliminating the whitebox:

# this your input area
enter1 = Entry(root, width=82, bg="Beige", borderwidth=0)

, at least on my system where the default background is approximately beige.

Also, I would only have the green bar up there when there's something to say. I.e., move the label3 stuff inside click().

Okay, I played with it for a while and came up with this, if BearofNH is correct ...

# a simple Tkinter number guessing game
 
from Tkinter import *
import random
 
def click():
    global rn
    # title
    root.title('Guess a number game')
    # get the number in the entry area
    num = int(enter1.get())
    guess = " (%s)" % num
    # check it out
    if num > rn:
        label3['text'] = 'Lower!' + guess
    elif num < rn:
        label3['text'] = 'Higher!' + guess
    else:
        label3['text'] = 'Guessed correctly!' + guess
        root.title('New game!')
        rn = random.randrange(1, 11)
    enter1.delete(0, 2)
 
 
root = Tk()
root.geometry("260x120+30+30")
# get the root background color
root_color = root['bg']
 
# pick a random integer from 1 to 10
rn = random.randrange(1, 11)
 
# let the user know what is going on
label1 = Label(root, text="Guess a number between 1 and 10")
label1.place(x=10, y=10) 
 
# prompt the user
label2 = Label(root, text="Enter your number: ")
label2.place(x=10, y=40)
 
# this your input area
enter1 = Entry(root, width=10, bg=root_color, relief='flat')
enter1.place(x=140, y=40)
enter1.focus()
 
# this is your mouse action button, right-click it to execute command
button1 = Button(root, text=" Enter ", command=click)
button1.place(x=160, y=40)
# or use the return key
enter1.bind('<Return>', func=lambda e: click())
 
# the result displays here
label3 = Label(root, text="", bg='green')
label3.place(x=10, y=75, width=200, height=20)
 
# start the mouse/keyboard event loop
root.mainloop()

Look, no while loop was needed! Also take a look at:
http://www.daniweb.com/techtalkforums/post366293-99.html
for a more GUI like solution.

Hi,
"You have it my dear Watson!" as Sherlock Holmes would say. This is better than good it's excellent again! But what I'd dearly love to know is, where do you get your info from as I couldn't find anything anywhere about the invisible "Enter" problem etc and couldn't work out how to circumvent the "Enter" button and use the keyboard enter. But not only have you done that, you're also using both keyboard & the "Enter" button to enter the number. Good one.:)
The other thing that you've worked out is that it's a console type of application that's scrollable, very similar to this one...http://www.daniweb.com/techtalkforums/thread75945.html... in that, as each number is typed in a new "frame" comes up with the results. At one stage I was calling this a "list box" type of application as I didn't know the right terminology to describe what was needed. Thank goodness we have a Mr. Vegaseat!:icon_cheesygrin:
But there is something else I'd like to know i.e. can one colourize a text box? I don't remember seeing any text boxes with any colourization of words, numbers etc in them in my googling, but I could be wrong and as everyone should know by now, fredzik being wrong is a very rare thing indeed!:icon_lol:

Thanks also to BearofNH for your invisible "Enter" help also.

fredzik.

Hi again again,

The above address has "about" in blue as part of the address but I don't think "about" is a part of the address.

fredzik

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.