Home > database >  How to separate a .py file for a tkinter frame?
How to separate a .py file for a tkinter frame?

Time:10-24

So, I have this code,

main.py:

from tkinter import Tk
from tkinter import Frame

class MainWindow():
    def __init__(self, master):
        self.master = master

        sw = self.master.winfo_screenwidth()
        sh = self.master.winfo_screenheight()

        w = 900
        h = 600

        x = (sw/2) - (w/2)
        y = (sh/2) - (h/2)
        
        self.master.geometry('%dx%d %d %d' % (w, h, x, y))
        self.master.resizable(False, False)

        #this part
        self.titleFrame = Frame(root, width=w, height=h-(h*0.92), bg="#1F2123")
        self.titleFrame.pack()
        #this part

if __name__ == "__main__":
    root = Tk()
    MainWindow(root)
    root.mainloop()

I want to separate this part (below) into another .py file and import it into my original main.py file

this is named to something like title_frame.py:

self.titleFrame = Frame(root, width=w, height=h-(h*0.92), bg="#1F2123")
self.titleFrame.pack()

(I added a comment in the original code if you want to see where it is)

For the reason as to why I need a separate .py file, well (1) I need it for code readability since I will have multiple frames and (2) it's my personal preference.

I just can't figure it out. Any help would be really appreciated!

CodePudding user response:

with question_import_001.py as:

from tkinter import Tk
from tkinter import Frame



def config(self, root,w,h):
    self.titleFrame = Frame(root, width=w, height=h-(h*0.92), bg="#1F2123")
    self.titleFrame.pack()

and main.py as:

from tkinter import Tk
from tkinter import Frame

from question_import_001 import config

class MainWindow():
    def __init__(self, master):
        self.master = master

        sw = self.master.winfo_screenwidth()
        sh = self.master.winfo_screenheight()

        w = 900
        h = 600

        x = (sw/2) - (w/2)
        y = (sh/2) - (h/2)
        
        self.master.geometry('%dx%d %d %d' % (w, h, x, y))
        self.master.resizable(False, False)

        # config(self, root,w,h)              # ->works !!!!!!!!!!!
        # pippo = config(self, root,w,h)      # ->works !!!!!!!!!!!
        self.pippo = config(self, root,w,h)   # ->works !!!!!!!!!!!

        # #this part
        # self.titleFrame = Frame(root, width=w, height=h-(h*0.92), bg="#1F2123")
        # self.titleFrame.pack()
        # #this part

if __name__ == "__main__":
    root = Tk()
    MainWindow(root)
    root.mainloop()

works to me as your initial main.py but I am not sure it makes sense or could be useful

using in main.py:

 `config(self.master, w, h)` or `pippo = config(self.master, w, h)    ` or `self.pippo = config(self.master, w, h)    `

and question_import_001.py as:

from tkinter import Tk
from tkinter import Frame

class config(Frame):

    def __init__(self, parent, w,h):
        Frame.__init__(self, parent)
        
        self.titleFrame = Frame(parent, width=w, height=h-(h*0.92), bg="#1F2123")
        self.titleFrame.pack()

or :

from tkinter import Tk
from tkinter import Frame

class config(Frame):

    def __init__(self, parent, w,h):
        super(config,self).__init__(parent)
        
        self.titleFrame = Frame(parent, width=w, height=h-(h*0.92), bg="#1F2123")
        self.titleFrame.pack()

works with me too

  • Related