Python Forum
Stop import from executing function, but allow the function to execute from tk btn.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stop import from executing function, but allow the function to execute from tk btn.
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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")
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you so much!
It worked
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,415 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,568 Apr-17-2020, 08:12 PM
Last Post: johnjh
  [Tkinter] Not able to use the output from one function into another function AravindPandian 1 1,633 Oct-17-2019, 08:40 AM
Last Post: Evil_Patrick
  [Tkinter] Button won't execute function TheLegendOfPanda 2 3,062 Jul-05-2019, 07:41 PM
Last Post: TheLegendOfPanda
  Stop Watch star/stop problem macellan85 1 2,551 Jun-12-2019, 06:04 PM
Last Post: woooee
  How to stop a tkinter function, without closing the window? keakins 5 13,035 Jun-29-2017, 11:53 AM
Last Post: keakins

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020