Home > other >  How to open a tkinter window that is in an other file in Python?
How to open a tkinter window that is in an other file in Python?

Time:01-17

I recently started working on a program in python using Tkinter and now I want to open it from another file. I have a home page file named HomePage.py that will have a button to open another file, called VirusTotalAPI.py. I didn't find any answers because when I run the program, it opens the VirusTotalAPI.py file, and if I close it the Homepage.py will run, but the button won't work and if I try to close it, it will open the HomePage.py.

#Homepage.py

from tkinter import *
import VirusTotalAPI as vt 

Home_Window=Tk()

Home_Window.geometry("980x530")
Home_Window.title("VirusTotal By Carotide")
Home_Window.config(background="grey")

def Window_35_mo() : 
    vt.Window_35mo
    
Window_35_mo_open = Button()
Window_35_mo_open.config(text= "Fichier < 35 mo", command= Window_35_mo)
Window_35_mo_open.pack()

Home_Window.mainloop()

The next one is a part from the VirusTotalAPI.py because the file is too long

#VirusTotalAPI.py
import requests
import hashlib
import json
from tkinter import *
from tkinter import filedialog
import HomePage

Window_35mo = Tk()
Window_35mo.geometry("980x530")
Window_35mo.title("VirusTotal By Carotide")
Window_35mo.config(background="grey")


global files
global file_path

def retrieve_API():
    API_Value=GetAPIBox.get("1.0","end-1c")
    print(API_Value)

GetAPIBox=Text(Window_35mo, height=2, width=10)
GetAPIBox.pack()
API_Button=Button(Window_35mo, height=1, width=10, text="YourAPI", 
                    command=lambda: retrieve_API())

API_Button.pack()
Window_35mo.mainloop()

Thank you in advance. I tried to import it by different ways like this:

import VirusTotalAPI

Or this:

from VirusTotalAPI import *

I tried to do this too:

from tkinter import *
from VirusTotalAPI import Window_35mo

Home_Window=Tk()

Home_Window.geometry("980x530")
Home_Window.title("VirusTotal By Carotide")
Home_Window.config(background="grey")
#homepage 
def winopen35mo() :
    Window_35mo
    
Window_35_mo_open = Button()
Window_35_mo_open.config(text= "Fichier < 35 mo", command= winopen35mo)
Window_35_mo_open.pack()

Home_Window.mainloop()

And it told me this:

ImportError: cannot import name 'Window_35mo' from partially initialized module 'VirusTotalAPI' (most likely due to a circular import)

CodePudding user response:

I finally found how to do it there is the solution :

First we need to import os, subprocess and sys

from tkinter import *
import os
import subprocess
import sys

Then,we declare the file path of the file, for this one it is VirusTotalAPI.py by doing so :

GUI_VirusTotalAPI_Path = 'C:\\Users\\...\\VirusTotalAPI.py'

Now we enter the args, to execute and enter the path name :

args = '"%s" "%s" "%s"' % (sys.executable,                  
                          GUI_VirusTotalAPI_Path,                     
                          os.path.basename(VirusTotalAPI)) 

We are almost there, now we create a function to run this using the args we previoulsy used :

def Open_GUI_VirusTotalAPI_35mo() :
   proc = subprocess.run(args)

Then another function to destroy the window :

def DestroyHomeWindow() : 
    Home_Window.destroy

Finally we create the button and "tell" it to execute the command Open_GUI_VirusTotalAPI_35moand at the same time close the window :

Window_35_mo_open = Button()
Window_35_mo_open.config(text= "Fichier < 35 mo", command= lambda:[Home_Window.destroy(),Open_GUI_VirusTotalAPI_35mo()])
Window_35_mo_open.pack()

And this is how I did it, sorry for the poor explanation and my bad english, hope this helped some people.

  • Related