from tkinter import *
class gui():
def __init__(self, window):
self.homePage()
Don't think I need to pass self into homePage but tried to see if that would make it work
def homePage(self):
label = Label(self.window,text='Hello')
label.pack()
Program will still run the window but without packing the label
def main():
window = Tk()
window.title('Morse Code Converter')
window.geometry('600x600')
window.mainloop()
if __name__ == '__main__':
main()
CodePudding user response:
Sorry to tell you; but the code you wrote is wrong in many ways. It defies the functionality of classes built in python. Also, the variables used in the class called self.window
is defined as null or None
. Here is a simple example:
import tkinter as tk
from tkinter.ttk import *
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
if (__name__ == "__main__"):
app = MainApplication()
app.mainloop()
EDIT:
If you want to use some type of "sub-packages", you can start with this tk.Label(self, text="{/n}").pack()
. Or you can do it manually by importing the needed packages by from tkinter import Label
CodePudding user response:
You need to create an instance of gui
class. Also you need to save the window
argument as an instance variable in order to use it inside homepage()
.
from tkinter import *
class gui():
def __init__(self, window):
self.window = window # save window argument for later use
self.homePage()
def homePage(self):
label = Label(self.window, text='Hello')
label.pack()
def main():
window = Tk()
window.title('Morse Code Converter')
window.geometry('600x600')
app = gui(window) # create an instance of gui class
window.mainloop()
if __name__ == '__main__':
main()