Python Forum
Stop import from executing function, but allow the function to execute from tk btn. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Stop import from executing function, but allow the function to execute from tk btn. (/thread-31395.html)



Stop import from executing function, but allow the function to execute from tk btn. - MrBitPythoner - Dec-08-2020

Hi,

I have python project I am building. It includes a main.py and several other .py files. When I import these other files into main.py, the import executes the functions from them. To stop this, i put an if __name__ == "__main__" statement in the functions of the other programs. However, I have a tkinter GUI with several buttons. These buttons execute functions from the other python files. how do i bypass the if __name__ == "__main__" so that the button can execute the command?

Thanks in advance
MrBitPythoner


RE: Stop import from executing function, but allow the function to execute from tk btn. - buran - Dec-08-2020

If you have functions they don't get executed at import. You need to call any imported function in order to execute it.
Maybe show your code (the two modules) and how you import one from the other. Best would be to keep the code as minimal as possible to reproduce your problem

my guess is - you actually call the functions without realizing it - e.g. when you assign it as call beck to a button.


RE: Stop import from executing function, but allow the function to execute from tk btn. - MrBitPythoner - Dec-08-2020

main.py

from tkinter import *
from search_book import *
from delete_book import *
from checkout_book import *
from add_book import add_book


root = Tk()
root.geometry("900x900")
root.title("Library Managment System")
##photo = PhotoImage(file = "imgs/book.png")
##root.iconphoto(False, photo)

top_label = Canvas(width=500, height=100, bg="Black", )
top_label.pack()
top_label.create_text(190,40,fill="white",font="Times 20 italic bold", text="Library Management System by Timware")


add_book_btn = Button(text="ADD A BOOK", width=50, height=5, bg="black", font="Courier 16", foreground="White", command=add_book(None))

add_book_btn.pack()
del_book_btn = Button(text="DELETE A BOOK", width=50, height=5, bg="black", font="Courier 16", foreground="White")
del_book_btn.pack()
search_book_btn = Button(text="SEARCH FOR A BOOK", width=50, height=5, bg="black", font="Courier 16", foreground="White")
search_book_btn.pack()
issue_book_btn =  Button(root, text="ISSUE A BOOK", width=50, height=5, bg="black", font="Courier 16", foreground="White")
issue_book_btn.pack()


root.mainloop()
add_book.py

from tkinter import *
from tkinter import messagebox
from pyautogui import prompt
from pyautogui import password
import mysql.connector


def add_book(event):
    if __name__ == '__main__':
        global password
        username = prompt("Username", "Enter your database username")
        password = password("Password", "Enter your database password")
        try:
            global conn
            conn = mysql.connector.connect(host="localhost", user=username, passwd=password, db="Library")
        except:
            messagebox.showerror("Incorrect username or password", "Access denied")
            exit()
        form = Tk()
        form.geometry("500x200")
        form.title("Add A New Book")
        top_lbl = Label(form, text="Add A Book", font="Courier 26").grid(column=4)
        title_lbl = Label(form, text="Title: ", font = "Courier 8")
        title_lbl.grid(row = 6, column = 3)
        global title_entry
        title_entry = Entry(form)
        title_entry.grid(row = 6, column=4)
        author_lbl = Label(form, text="Author: ", font = "Courier 8")
        author_lbl.grid(row = 7, column = 3)
        global author_entry
        author_entry = Entry(form)
        author_entry.grid(row = 7, column=4)
        id_lbl = Label(form, text="ISBN: ", font = "Courier 8")
        id_lbl.grid(row=9, column=3)
        global id_entry
        id_entry = Entry(form)
        id_entry.grid(row=9, column=4)
        submit_btn = Button(text="SUBMIT", command=submit)
        submit_btn.grid(row=10, column=4)

    def submit():
        title = title_entry.get()
        author = author_entry.get()
        ISBN = id_entry.get()
        cur = conn.cursor()
        cur.execute("INSERT INTO Books (ISBN, ITLE, AUTHOR) VALUES (%s, %s, %s);", (ISBN, title, author))
        conn.commit()
        messagebox.showinfo("Created", "Book has been added to the database")



RE: Stop import from executing function, but allow the function to execute from tk btn. - buran - Dec-08-2020

As I expected - you call the function when you assign it as a callback

command=add_book(None) should be just command=add_book

As a side note - your code is really poorly organized (real spaghetti code - no offence)
the use of global variables is considered bad practice, you declare them global all over the place, you mix the logic and the gui, etc.
you really need to rethink how your code is organized.


RE: Stop import from executing function, but allow the function to execute from tk btn. - MrBitPythoner - Dec-08-2020

Thank you so much!
It worked