2
  • I have three widgets (shown below) - a label, an entry, and a button.

enter image description here

  • I'm hoping to accept user input of an integer when the button is clicked.
  • I get the following error: ERROR RESOLVED! I was making this too complicated. Success is Mine!!!

enter image description here

  • I believe that my problem is related to this:

Stack Overflow Post on get()

  • I chopped it down to the minimum amount of code possible to achieve this before posting.

Any Ideas or obvious failures you see would be greatly appreciated.

Screenshot of screen #1 (should eventually progress to screen #2 after all the players are entered. Screen #2 is where the game will begin.):

Screen #1

Working Code:

from tkinter import *
import tkinter
from PIL import Image, ImageTk
# The next 2 imports override the basic Tk widgets
# widget options that use fg and bg will no longer work. 
# Must use, instead, ttk.Style class
from tkinter import ttk
from tkinter.ttk import *
# See docs.python.org/8/library/tkinter.ttk.html

# Class Based Windows
# youtube.com/watch?v=RkaekNkIKNY

# Tkinter - GUI Eaxample Multiple Display Frames
# youtube.com/watch?v=KdoOm3xo8X0


def main():
    root = tkinter.Tk()
    window1 = Window(root, "Play_a_game_of_Pig", "969x690", "someMessage",
                     "pig.ico", 0)
    return None


class Window:
    number_of_players = 0
    # int player_names[] = new player_names[number_of_players]

    def __init__(self, root, title, geometry, message, iconbitmap,
                 playerNumber):

        self.root = root  # this is the instance variable for the entire window
        self.root.title(title)
        self.root.geometry(geometry)
        self.root.iconbitmap(iconbitmap)
        self.number_of_players = playerNumber
        # Make the window show up in center of screen and not be ccovered up by anything else.
        # self.root.eval('tk::PlaceWindow %s center' % self.root.wininfo_toplevel())
        # this is an important line.
        # self.root.mainloop()

        def addPlayers():
            """Allows user to input the number of and names of all game players"""
            # stackoverflow.com/questions/12169258/should-i-use-entrys-get-or-its-textvariables-for-tkinter-in-python

            print("\n initial # of players = " + str(self.number_of_players))

            # Collects user input from the entry and turns it into an int
            # user_input_number_of_players.set(int(str(entry_player_number.get("1.0", 'end-1c'))))
            try:
                user_input_number_of_players = int(entry_player_number.get())
                print("Inside try block, user_input = ")
                print(user_input_number_of_players)
                self.number_of_players = user_input_number_of_players

            except ValueError:
                tkinter.messagebox.showerror('Non-Integer Input', 'User MUST enter a player # greater than 1.', icon = 'error')
                # tkinter.messagebox.deiconify()
                # tkinter.messagebox.quit()
                # tkinter.messagebox.destroy()
                
            
            #user_input_number_of_players.set(int(str(entry_player_number.get("1.0", 'end-1c'))))

            # Set class instance value to this input from the user
            # self.number_of_players = user_input_number_of_players

            print("# of players after click = " + str(self.number_of_players))

            return self.number_of_players
        
        print("# of players after click = " + str(self.number_of_players))
        
        # Add a label

        myLabel1 = tkinter.Label(self.root, text="Please Enter # of Players",
                                 width=25)
        myLabel1.config(font="Courier 14 bold")
        myLabel1.grid(row=2, column=1)

        # bind user input to a variable from the entry box.
        # Specifies a name whose value is linked to the widget value.

        user_input_number_of_players = tkinter.StringVar()

        # add an entry box

        entry_player_number = tkinter.Entry(self.root, width=5, borderwidth=5,
                                            textvariable=user_input_number_of_players)
        # number.set(int("0"))
        entry_player_number.grid(row=2, column=3, rowspan=2)
        # specify a default value inside the entry box
        # entry_player_number.insert(0,int("2"))

        # Add a button for adding players to the game

        addPlayerButton = tkinter.ttk.Button(self.root,
                                     text="Enter",
                                     command=addPlayers)

        addPlayerButton.grid(row=2, column=4)

        self.root.mainloop()

        pass

    pass


main()

In Summary, I first made two screens. I'm hoping to make these into classes. The first screen (pictured) should get input from the user. Then, the first screen should progress to the second screen.

To do this, I have used the following resources:

Resources for merging this code together into Multiple class based windows and the backend:

j_4321
  • 15,431
  • 3
  • 34
  • 61
heather
  • 113
  • 1
  • 12
  • Do you expect it to open a new window like `Toplevel()` – Delrius Euphoria Aug 31 '20 at 16:20
  • Let me edit and provide some additional info that might be helpful. I'm trying not to be too long winded. In short, no. It doesn't need to have a Top Level widget. But, it should open a window that can navigate to a new window/frame. I'm flexible. My main goal is was to make it open a window that accepts user input that can persist throughout the applications life. – heather Aug 31 '20 at 16:51

0 Answers0