Search code examples
pythonpython-3.xtkintersoftware-designtkinter-canvas

can i create 2 module for a single page by using tkinter?


i want to make a single page by using Tkinter moreover I want to make it in 2 modules. in that case, I can simplify the code

the code that I've made is

module 1 (a1.py)

from tkinter import *
from a2 import frame


root=Tk()
root.title("voccabulary journel")
root.geometry("700x450")
root.configure(bg='#ff8000')


frame()



root.mainloop()

module 2(a2.py)

from tkinter import *



def frame():
    Grid.rowconfigure(root,0,weight=1)
    Grid.columnconfigure(root,0,weight=1)
    Grid.columnconfigure(root,1,weight=3)

    frame1=Frame(root,background='black')
    frame1.grid(row=0,column=0,sticky='nsew')

    frame2=Frame(root,background='white')
    frame2.grid(row=0,column=1,sticky='nsew')

    labeltry1=Label(frame1, text='testing')
    labeltry1.pack()

    labeltry2=Label(frame2,text='tersting')
    labeltry2.pack()

I could have written in one module but I just want to simplify it..

i will attach the image of the terminal anyway

enter image description here


Solution

  • There is good rule: send all values explicitly as arguments.

    And this is your problem - in frame() you use root which you didn't send as argument.

    Use:

    a2.py

    def frame(root):
        # code
    

    a1.py

    frame(root)
    

    And this resolves your problem and makes code more readable and simpler to debug.