简体   繁体   中英

How to import another python file as many times as the button is pressed in tkinter?

def addstudent():
   import add_student


b1=  Button(root, text="Add Student",width=35,font=("Times New Roman",15),command=addstudent)

b1.place(relx=0.5,rely=0.24,anchor=CENTER)

I was trying to import add_student as many times as the button is pressed. When button is pressed first time the function addstudent imports add_student file. But when button is pressed next time that function don't imports add_student file.

I'm not sure what kind of result this would do but I think you want you call to add_student to have some kind of "side-effect" (add a row to a database, add an element to a list, ...).

It's true that sometimes importing a name will have side effects but this might now be the case.

You most likely have to call the function using the name you have imported such as in the following code.

def addstudent():
    import add_student # make the name add_student visible in the function
    add_student() # call the function and possibly do some work, including side-effects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM