0

I'm trying to have the program open the window after several options are input and a 'continue' button is pressed.

The code below is the if statement to sift through the options and if the correct sequence is pressed it will open a window, I made but it's not opening the file. I'm not sure if that's anywhere near the right code, but there are no errors and didn't open the file). Im trying to open the file so it is usable I.e.: a new tkinter screen that youcan interact with.

if price == 'one':
    if periph == 'one':
        if Lights == 'one':
            exec(open('C:\\Tkinter\\CBG_Save_Prebuild_Screen.py'))
Rhys34512
  • 39
  • 5
  • Please don't `exec` a Python file. `import` it instead. – OneCricketeer Aug 06 '20 at 00:56
  • Difficult to be specific because your question lacks a lot of information…however you can create new tkinter windows whenever you want and for whatever reasons (i.e. due to `if` statements) by calling [`Toplevel()`](https://web.archive.org/web/20190429194251id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/toplevel.html) to create them, Note that this is done automatically for you when you call `Tk()` to initialize the module. – martineau Aug 06 '20 at 00:58
  • Hi, I am trying to open a window that I have already made. Im trying to open it once the 'if' statement has been fulfilled so basically the exec(open(c: _____) line is the line of code im currently using to try and open that file im not sure if thats the right code or what code I need to do that. Hope that explains it a bit better. im quite new to coding so I dont know too much. Thanks for the help – Rhys34512 Aug 06 '20 at 01:10
  • Are you sure that your code even reaches the exec statement on correct sequence. Because if it did, I think it should have thrown an error, based on information in [this](https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3) post. Correct line being - exec(open('C:\\Tkinter\\CBG_Save_Prebuild_Screen.py').read()) – Harsh Aug 06 '20 at 01:16
  • I tried that however it doesnt give me an error or open the screen up. Im trying to have it so it will open the screen up so you can use it if that makes sense. – Rhys34512 Aug 06 '20 at 01:19
  • Is your code reaching the exec statement? Try something like `print('here')` before the exec statement, and please confirm if you get the output 'here' on console. – Harsh Aug 06 '20 at 01:44
  • It does print 'here' to the console so it does reach the exec statement but it doesnt work for some reason. – Rhys34512 Aug 06 '20 at 01:56
  • to opem a tkinter window, u use `Tk()` here u want to run a python script that has `tkinter`, if im not wrong – Delrius Euphoria Aug 06 '20 at 07:38
  • Yeah im wanting it to open to a tkinter screen ive made – Rhys34512 Aug 06 '20 at 08:08

2 Answers2

0

1. Method
You can just import CBG_Save_Prebuild_Screen.py like this

import CBG_Save_Prebuild_Screen 

or

import .CBG_Save_Prebuild_Screen

or

from .CBG_Save_Prebuild_Screen import *

EDIT

If you use OOP for tkinter you can just add {Your tkinter class} in the

if __name__ == "__main__":

but if you dont use the OOP, you can just insert your widget into a def in the CBG_Save_Prebuild_Screen.py and then call your widget in the if statement

2. Method
You can just execute the file, like this

import os               

os.system("python CBG_Save_Prebuild_Screen.py") #For python 2         
os.system("python3 CBG_Save_Prebuild_Screen.py") #For python 3

Happy coding!

Aryasatya
  • 36
  • 1
  • 3
0

For your specific problem you can try this

if price == 'one':
    if periph == 'one':
        if Lights == 'one':
              import CBG_Save_Prebuild_Screen.py
#if both your.py file are in same folder

You can do this also by os.startfile()

The os.startfile() method allows us to start a file with its associated program. In other words, we can open a file with it’s associated program, just like when you double-click a PDF and it opens in Adobe Reader.

import os
if price == 'one':
    if periph == 'one':
        if Lights == 'one':     
                  
              path='C:\\Tkinter\\CBG_Save_Prebuild_Screen.py'
              os.startfile(path)

I think it may help you

Ujjwal Dash
  • 767
  • 1
  • 4
  • 8
  • Hello, I tried the first method you have and it doesnt give me an error but it also doesnt open the file. The second method comes up with an error: path='C:\\Tkinter\\CBG_Save_Prebuild_Screen.py')) – Rhys34512 Aug 06 '20 at 06:04
  • Opps I have forgot to remove those ```))``` now I have edited it you can try the 2 nd one – Ujjwal Dash Aug 06 '20 at 06:30
  • Did you close the 2nd window you have created with ```.mainloop()``` – Ujjwal Dash Aug 06 '20 at 06:32
  • Using method 2 will execute the script as a new process, so you cannot interact with it as OP requests: *a new tkinter screen that youcan interact with*. – acw1668 Aug 06 '20 at 06:57
  • It just opens the file it doesnt actually open it so it will show the tkinter window. im trying to have it open the tkinter window when the if statement is fulfilled. – Rhys34512 Aug 06 '20 at 08:08