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()
Answer from Bryan Oakley on Stack OverflowHow to make a window with buttons in python - Stack Overflow
class - Creating buttons with Python GUI - Stack Overflow
How To Create a Button Input
Python tkinter button as input
Videos
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.
You need your "constructor" method to be named __init__, not _init_. As it is written, your grid and create_widgets methods never get called since _init_ never gets called.
OK, first problem is that you have declared your following code:
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()code here
inside the class itself. It should be outside, so this an indentation problem (maybe stackoverflow problem with indents?).
secondly I simplified the code to get it to run
from Tkinter import *
class Application(Frame):
"""A GUI application with three button"""
#create a class variable from the root (master):called by the constructor
def _init_(self, master):
self.master = master
#simple button construction
# create a button with chosen arguments
# pack it after the creation not in the middle or before
def create_widgets(self):
#"""Create three buttons"""
#Create first button
btn1 = Button(self.master, text = "I do nothing")
btn1.pack()
#Create second button
btn2 = Button(self.master, text = "T do nothing as well")
btn2.pack()
#Create third button
btn3=Button(self.master, text = "I do nothing as well as well")
btn3.pack()
#must be outside class definition but probably due to stackoverlow
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
#call the method
app.create_widgets()
root.mainloop()
This is a starting point and definitely works as proven below:

You can probablty muck around with the grid() instead of pack and call the method from the def init constructor. Hope it helps.
This calling method also works:
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root).create_widgets() #creates and invokes
root.mainloop()
My final try also works:
def __init__(self,master):
self.master = master
self.create_widgets()
followed by:
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()

The final code:
from Tkinter import *
class Application(Frame):
"""A GUI application with three button"""
def __init__(self,master):
self.master = master
self.create_widgets()
def create_widgets(self):
#"""Create three buttons"""
#Create first buttom
btn1 = Button(self.master, text = "I do nothing")
btn1.pack()
#Create second button
btn2 = Button(self.master, text = "T do nothing as well")
btn2.pack()
#Create third button
btn3=Button(self.master, text = "I do nothing as well as well")
btn3.pack()
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()

I understand this is a beginner question but I am creating a desktop game because I really, REALLY, have nothing better to do. Here is the base code.
count = 1000
while True;
print{f"Bottle(s) left: {count}"}
if count > 0
count -= 1
time.sleep(1)
else:
count = 1000
print("All bottles have been seized")
time.sleep(5)
breakSo my guess is that might have to put that in {} brackets and create some sort of input, but I do not no. I also have a second piece which requires a different button input. All it needs to do is display the number going down on two different objects as two different buttons are pressed, those being the spacebar, and the D key.