2

I am trying to create a game that allows the user to input a "username" through an entry widget. Setting aside the whole game aspect, for now I just need a program that loads up a title screen, then on clicking a button a new "setup" window appear prompting me to enter my username, then on clicking a button there the setup window goes away and I have my game window, with a label that reads "USERNAME: [your name here]".

Can anyone help me with this?

(EDIT: Here is the code I'm currently working on

from tkinter import *
import random
import os
import time
import math

def cargarimagen(nombre):
    ruta=os.path.join('Images',nombre)
    imagen=PhotoImage(file=ruta)
    return imagen

def operacion():
    num1=random.randint(0,255)
    num2=random.randint(0,255)
    op=random.randint(0,4)    
    if op == 0:
        prompt= (hex(num1)[2:].upper() + " + " + hex(num2)[2:].upper())
        answer= num1 + num2
    if op == 1:
        prompt= (hex(num1)[2:].upper() + " - " + hex(num2)[2:].upper())
        answer= num1 - num2
    if op == 2:
        prompt= (hex(num1)[2:].upper() + " C16")
        answer= (16**2) - num1
    if op == 3:
        prompt= (hex(num1)[2:].upper() + " C15")
        answer= (16**2) - num1 - 1
    return prompt


def changeName():
    name = setup.yourName.get()
    GameWindow.labelText.set(name)
    setup.yourName.delete(0, END)
    setup.yourName.insert(0, ' ')
    return

def play():
    return GameWindow() + changeName()

root= Tk()
root.title('Polyominos Start Menu')
root.minsize(640,480)
root.maxsize(640,480)

class MainWindow:
    def __init__(self,parent):
        self.contenedor=Canvas(parent,width=640,height=480)
        self.contenedor.place(x=0,y=0)
        self.foto=cargarimagen('Tetris.gif')
        self.label_fondo=Label(self.contenedor,image=self.foto)
        self.label_fondo.place(x=0,y=0)
        self.but=Button(root, text="START",font="Helvetica",width=10,height=2,bg='White',command=SetupWindow)
        self.but.place(x=213,y=413)
        self.about=Button(root, text="ABOUT",font="Helvetica",width=10,height=2,bg='White',command=AboutWindow)
        self.about.place(x=320,y=413)


def SetupWindow():
    setup=Toplevel()
    setup.minsize(124,190)
    setup.maxsize(124,190)
    setup.title('Set Up')
    frame = Frame(setup,bg ="White" )
    frame.grid()
    lab1= Label(frame, text='Enter your name:', bg ="White").grid(column=0, row=0)

    custName = StringVar(None)
    yourName = Entry(frame, textvariable=custName, bg ="White").grid(column=0, row=1)

    lab2= Label(frame, text=' ', bg ="White").grid(column=0, row=2)
    lab3= Label(frame, text='Pick a difficulty:', bg ="White").grid(column=0, row=3)
    diff = StringVar()
    diff.set(None)
    radio1 = Radiobutton(frame, text='Easy', value='Easy', variable = diff, bg ="White").grid(column=0,row=4)
    radio1 = Radiobutton(frame, text='Medium', value='Medium', variable = diff, bg ="White").grid(column=0,row=5)
    radio1 = Radiobutton(frame, text='Hard', value='Hard', variable = diff, bg ="White").grid(column=0,row=6)
lab4= Label(frame, text=' ', bg ="White").grid(column=0, row=7)
    playnow= Button(frame, text='READY', command=play, bg ="White").grid(column=0,row=8)
lab5= Label(frame, text=' ', bg ="White").grid(column=0, row=9)


def GameWindow():
    root.withdraw()
    GameWindow=Toplevel()
    GameWindow.minsize(595,300)
    GameWindow.maxsize(595,300)
    GameWindow.title('Polyominos')
    operaciones=Frame(GameWindow,width=300,height=300, bg='white', highlightthickness=5, highlightbackground='black')
    operaciones.place(x=0, y=0)

    labelText = StringVar()
    labelText.set('USER')
    usrname = Label(operaciones, textvariable=labelText)
    usrname.place(x=80, y=20)

    cubos=Frame(GameWindow,width=300,height=300, bg="white", highlightthickness=5, highlightbackground='black')
    cubos.place(x=295, y=0)


def AboutWindow():
    about=Toplevel()
    about.minsize(137,94)
    about.maxsize(137,94)
    about.title('About Polyominos')
    frame1 = Frame(about,bg ="White" )
    frame1.grid()
    lab0 = Label(frame1, text="David Salazar Quintana",bg ="White").grid(column=0,row=0)
    lab1 = Label(frame1, text="ITCR",bg ="White",).grid(column=0,row=1)
    lab2 = Label(frame1, text="Ingeneria en computadores",bg ="White").grid(column=0,row=2)
    lab3 = Label(frame1, text="Profesor: Milton Villegas",bg ="White").grid(column=0,row=3)
    lab4 = Label(frame1, text="Version: 1.0 (Abril 2012)",bg ="White").grid(column=0,row=4)





Botones_Principales=MainWindow(root)    
mainloop()
root.destroy()

Sorry for the code being half in spanish, you should still get what's going on though).

Salazar
  • 21
  • 2

1 Answers1

0

I hope this piece of code will able to help you figure out how to implement the pop up text dialog box. The way of this example work by implementing a button in the main window (root), when you click on it, the pop up dialog will appear MyDialog class object, will be created, then it will use wait_window() to wait until it is finish. You can see how the pop up dialog box is implemented, it is an simple TopLevel widget, it is just like another frame you can think of, you pack your label, your Entry field, and finally a button mySubmitButton.

Look down to the send function, it is pretty much just getting the entry using simple .get() method from the Entry. And then you will destory() the window, and resume back the main window. You can do it as many time as you want.

import tkinter as tk

class MyDialog:
    def __init__(self, parent):
        top = self.top = tk.Toplevel(parent)
        self.myLabel = tk.Label(top, text='Enter your username below')
        self.myLabel.pack()

        self.myEntryBox = tk.Entry(top)
        self.myEntryBox.pack()
        self.mySubmitButton = tk.Button(top, text='Submit', command=self.send)
        self.mySubmitButton.pack()

    def send(self):
        global username
        username = self.myEntryBox.get()
        self.top.destroy()

def onClick():
    inputDialog = MyDialog(root)
    root.wait_window(inputDialog.top)
    print('Username: ', username)

username = 'Empty'
root = tk.Tk()
mainLabel = tk.Label(root, text='Example for pop up input box')
mainLabel.pack()

mainButton = tk.Button(root, text='Click me', command=onClick)
mainButton.pack()

root.mainloop()

enter image description here

In the sample output I did it two times. Entered George and Sam (my friend) as username, and the username will be updated everytime you open up a new dialog box. Output:

>>> ================================ RESTART ================================
>>> 
Username:  George
Username:  Sam

Edit: The submit button seems to be buggy? It sometimes doesn't want to be appear. It is clickable, but it does not appear until it is clicked. It appears to be only problem for Mac OS.

Reference used: effbot, stackoverflow, tutorialspoint, daniweb

Community
  • 1
  • 1
George
  • 4,514
  • 17
  • 54
  • 81