Object Oriented Programming Crash course with Python 3 - Tkinter tutorial Python 3.4 p. 2
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about Tkinter class structure, __init__ and self, and the multi-frame app pattern the video builds.
Recommended by 3 people across 3 different months, 2017 to 2020.
What it is
Part 2 of sentdex's Tkinter GUI series, which doubles as a rough crash course in object oriented programming in Python 3. It walks through classes, __init__, self and parent while building a multi-frame Tkinter app.
What people said
Recommends Anirudh Sridhar · 2017-06 · Stack Overflow · on “Tkinter moving balls program creating 4 balls instead of 2”
You are initializing two balls in your __init()__ method. self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red") self.ball2 = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,fill="red") When the object is created for the class two balls instead of one. So changing the init method in the class should fix it. You need to create two different objects for two different balls, rather than two different variables in the class itself. class Ball: #creates balls def __init__(self, canvas, x1,y1,x2,y2): self.x1 = x1 self.y1 = y1 self.x2 = x2 self.y2 = y2 self.canvas = canvas self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red") Also, you have two
Recommends Blake O'Hare · 2018-01 · Stack Overflow · on “Python/ Pygame: Unsure where to add new lines of code”
The quick and direct answer to your question is that classes can be added anywhere in your code before you use them (generally in the beginning without being nested under anything else) and then you'd create it later in your code (but outside of your game loop) with a player = Player() call. It looks like you might be starting with programming and running into OO for the first time. As for the longer answer, I can't tell you a general formula of "where new code goes", but gathering from the nature of your question, I more or less agree with one of the existing comments that you should probably do some reading on classes and object oriented programming. When it comes to writing code, every si
Recommends jimbob88 · 2020-05 · Stack Overflow · on “How to stop my Tkinter window from freezing?”
I have assumed your Test1 and Test2 functions are somewhat infinite so I have created this as the format for my Test1.py and Test2.py : # Test1.py / Test2.py import tkinter as tk def func(): while True: root = tk.Tk() root.mainloop() Now with your code I would thoroughly recommend moving away from your current format and moving to Object Oriented Programming [this video] as it will save you many headaches in the future! This here is the code I have written to make it work: import tkinter as tk import tkinter.ttk as ttk import Test1 import Test2 class reco_win: def __init__(self, master): self.master = master self.master.geometry('375x100') self.master.configure(background="LightBlue2") style = tt
Cites Ozzy Kids · 2019-10 · Stack Overflow · on “Is self a requirement in __init__ and other methods/functions?”
I'm trying to make a tkinter GUI with multiple frames and when creating the class I am including the __init__ method . Do I have to pass self into this or is it implied? I am watching a tutorial for tkinter ( [this video] ). He mentions that self is implied and that you don't have to use it, however my teacher was teaching us that you always need to include self. Would it still work without self or is it required? Furthermore, he says it can be called whatever you want, but I have never seen anyone use other names. class MathematicalQuizApp(tk.Tk): def __init__(self, *args, *kwargs): tk.Tk.__init__(self, *args, **kwargs) container = tk.Frame(self) container.pack(side="top", fill="both", expand =
Cites OogieM · 2020-06 · Stack Overflow · on “Python Tkinter change app menu command works at root but not when inside a frame class”
I'm trying to follow along with setting up an app that will have multiple windows I stack and then just bring to front the one that gets called. It's based on this series of videos [this video] I want to change the app name and the standard File and Help menus when it runs on Mac systems. When I use this structure it all works: if platform == 'darwin': from Foundation import NSBundle bundle = NSBundle.mainBundle() if bundle: info = bundle.localizedInfoDictionary() or bundle.infoDictionary() if info and info['CFBundleName'] == 'Python': info['CFBundleName'] = "LambTracker" root = tk.Tk() root.title ("LambTracker") root.geometry("1200x900") root.minsize(1000, 750) mainmenu = tk.Menu(root) appmenu =
Cites Mackenzie Frazer · 2020-08 · Stack Overflow · on “AttributeError: module 'tkinter' has no attribute 'Tk__init__'”
def __init__(self, *args, **kwargs): tk.Tk__init__(self, *args, **kwargs) container=tk.Frame(self) container.pack(side='top',fill='both',expand=True) continer.grid_rowconfigure(0,weight=1) container.grid_columnconfigure(0,weight=1) Help me please I watch it on [this video] but it did not work on me