0

I am practicing to create a small project with different files to have a clean code. I want to show yellow frame from (fyellow.py) into (main.py) and input a label into it from (funbut.py) using Button's function. This is my code example: (3 Python files - main.py, fyellow.py, and funbut.py)

  • main.py

    from tkinter import *
    from fyellow import *
    import funbut
    
    root = Tk()
    root.geometry("500x500")
    
    # Show Yellow Frame into Main from (fyellow.py)
    myframe = Frameyellow(root)
    
    # Button with command - But_fun1
    but1 = Button(root, text="Text",command=funbut.but_fun1)
    but1.pack()
    
    
    root.mainloop()
    
  • funbut.py

    from tkinter import *
    from fyellow import *
    
    # Function of Button (but1) PROBLEM HERE! (ERROR - 'framey' is not defined)
    def but_fun1():
        label1 = Label(framey,text="LabelText")
        label1.place(x=10,y=10)
    
  • fyellow.py

    from tkinter import *
    
    class Frameyellow:
        def __init__(self,rootyellow):
            self.rootyellow = rootyellow
            self.framey = Frame(rootyellow, width=200,height=200,bg="yellow")
            self.framey.pack()
    

Could explain what can I do to use the self.framey from file (fyellow.py) to avoid
error 'framey' is not defined?

martineau
  • 119,623
  • 25
  • 170
  • 301
Loro Bil
  • 3
  • 1
  • well.... `framey` is not defined, do you see any place where it is defined? use function arguments to pass master to that label, also `myframe` is not a `Frame` – Matiiss Oct 31 '21 at 17:39
  • Kindly could you please arrange my code how can I do it ? I appreciate your help so much. @Matiiss – Loro Bil Oct 31 '21 at 17:44
  • Kindly could you please recommend to me , any online links or books for python Tkinter ? (From beginner to Professional ) I am watching you tube videos .. @Matiiss – Loro Bil Oct 31 '21 at 18:27
  • well you can search for documentation for specific libraries if you need but otherwise if you are a beginner just keep on watching youtube, if you want to learn more I would also suggest practice which this pretty much is, take a look at what other people have made and maybe learn from them if that is what you were asking about – Matiiss Oct 31 '21 at 18:28

1 Answers1

0

So main.py file would look like this:

from tkinter import Tk, Button
from fyellow import FrameYellow
from funbut import place_label

root = Tk()
root.geometry("500x500")

my_frame = FrameYellow(root)
my_frame.pack()

but1 = Button(root, text="Text", command=lambda: place_label(my_frame))
but1.pack()

root.mainloop()

fyellow.py like this (tho kinda pointless to create a class whose sole purpose is to have the frame a different color, just use arguments and create a normal frame):

from tkinter import Frame


class FrameYellow(Frame):
    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs, bg='yellow')

and funbut.py should be sth like this:

from tkinter import Label


def place_label(parent, text='Text', pos=(0, 0)):
    Label(parent, text=text).place(x=pos[0], y=pos[1])

Also:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • 1
    @Loro Bil: Also see [Should wildcard import be avoided?](https://stackoverflow.com/questions/3615125/should-wildcard-import-be-avoided) – martineau Oct 31 '21 at 18:07