How do I create a function that makes a window with two buttons, where each button has a specified string and, if clicked on, returns a specified variable? Similar to @ 3:05 in this video https://www.khanacademy.org/science/computer-science-subject/computer-science/v/writing-a-simple-factorial-program---python-2 (I know it's a tutorial for a very easy beginners program, but it's the only video I could find) but without the text box, and I have more controls over what the 'ok' and 'cancel' buttons do.
Do I have to create a window, draw a rect with a string inside of it, and then make a loop that checks for mouse movement/mouse clicks, and then return something once the mouse coords are inside of one of the buttons, and the mouse is clicked? Or is there a function/set of functions that would make a window with buttons easier? Or a module?
Overview
No, you don't have to "draw a rect, then make a loop". What you will have to do is import a GUI toolkit of some sort, and use the methods and objects built-in to that toolkit. Generally speaking, one of those methods will be to run a loop which listens for events and calls functions based on those events. This loop is called an event loop. So, while such a loop must run, you don't have to create the loop.
Caveats
If you're looking to open a window from a prompt such as in the video you linked to, the problem is a little tougher. These toolkits aren't designed to be used in such a manner. Typically, you write a complete GUI-based program where all input and output is done via widgets. It's not impossible, but in my opinion, when learning you should stick to all text or all GUI, and not mix the two.
Example using Tkinter
For example, one such toolkit is tkinter. Tkinter is the toolkit that is built-in to python. Any other toolkit such as wxPython, PyQT, etc will be very similar and works just as well. The advantage to Tkinter is that you probably already have it, and it is a fantastic toolkit for learning GUI programming. It's also fantastic for more advanced programming, though you will find people who disagree with that point. Don't listen to them.
Here's an example in Tkinter. This example works in python 2.x. For python 3.x you'll need to import from tkinter
rather than Tkinter
.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# create a prompt, an input box, an output label,
# and a button to do the computation
self.prompt = tk.Label(self, text="Enter a number:", anchor="w")
self.entry = tk.Entry(self)
self.submit = tk.Button(self, text="Submit", command = self.calculate)
self.output = tk.Label(self, text="")
# lay the widgets out on the screen.
self.prompt.pack(side="top", fill="x")
self.entry.pack(side="top", fill="x", padx=20)
self.output.pack(side="top", fill="x", expand=True)
self.submit.pack(side="right")
def calculate(self):
# get the value from the input widget, convert
# it to an int, and do a calculation
try:
i = int(self.entry.get())
result = "%s*2=%s" % (i, i*2)
except ValueError:
result = "Please enter digits only"
# set the output widget to have our result
self.output.configure(text=result)
# if this is run as a program (versus being imported),
# create a root window and an instance of our example,
# then start the event loop
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
You should take a look at wxpython, a GUI library that is quite easy to start with if you have some python knowledge.
The following code will create a window for you (source):
import wx
app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
frame.Show(True) # Show the frame.
app.MainLoop()
Take a look at this section (how to create buttons). But start with the installation instructions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With