I am wondering on how to make a very simple gui with the ability for the user to click a button and perform the ability as if it were a user input. (ie raw_input). I would also like it to have the title of the program (game) on top and the ability to exit.
I don't actually know what you mean by "as if it were a user input", but here's how you would make a simple GUI with a exit button, perform button and the title set. Also, for more GUI examples, check out the "Python GUI programming"-topic. Hope this helps!
#Simple GUI example, #made by Tyyppi_77 try: #Python3 import tkinter as tk except ImportError: #Python2.x import Tkinter as tk #Your main window root = tk.Tk() title = "Put your title here" root.title(title) def perform(): #Put your action here pass #Create a perform button p_button = tk.Button(root, text="Do Something", command=perform) #Show it p_button.pack() #Create an exit button e_button = tk.Button(root, text="Exit", command=root.destroy) #Show it e_button.pack() #Start the GUI mainloop root.mainloop()
The Tkinter GUI toolkit's equivalent to raw_input() is the Entry widget. Here is an example:
# simple Tkinter code # using two labels, an entry and a button # make tk the name space regardless of Python version try: # Python2 import Tkinter as tk except ImportError: # Python3 import tkinter as tk def action(): """ get the entry string reverse string using slicing [::-1] show the result """ name = enter.get() label2.config(text=name[::-1]) # create the root window root = tk.Tk() root.title("My title") # create all the widgets # the prompt label label1 = tk.Label(root, text='Enter name:', width=30) # the entry enter = tk.Entry(root, bg='yellow') # the action button button = tk.Button(root, text='Reverse name', command=action) # the result label label2 = tk.Label(root, text='') # pack widgets vertically in the root window in order label1.pack(side='top') enter.pack(side='top') button.pack(side='top') label2.pack(side='top') # start cursor in entry enter.focus() # start the event loop root.mainloop()
You exit via the root window upper corner x
Another option is to use Tkinter's simple dialogs for user input, and to some extent output too:
# use Tkinter's simple dialogs (pop-up) to ask for input # minvalue and maxvalue are optional try: # for Python2 import Tkinter as tk import tkSimpleDialog as tksd except ImportError: # for Python3 import tkinter as tk import tkinter.simpledialog as tksd root = tk.Tk() # show input dialogs without the Tkinter window root.withdraw() # if a non-numeric value is entered, will prompt again price = tksd.askfloat("Price", "Enter the price of the item:") print(price) # use askfloat or askinteger (whole items only) quantity = tksd.askinteger("Quantity", "Number of items needed:", minvalue=1, maxvalue=1000) print(quantity) result = "Total cost is %0.2f" % (price*quantity) print(result) # you could use askstring to display the result sf = "%0.2f" % (price*quantity) tksd.askstring("Result", "Total cost is:", initialvalue=sf)