Python Forum

Full Version: Python script on windows 10 shuts down
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My python script runs on Windows 10. It uses various libraries including tkinter.
Opening it in idle, it runs perfectly.
I right click on the script and associate it with python. When I double click the script it opens as a tkinter window with buttons, then a file manager opens as programmed, and I select a file, but the script then closes.
When I open the script in idle, the program continues to work when the file is selected.
I have tried both as a .py and a .pyw. This makes no difference
I include an example below. I omitted some of the def's to see if one of them was the problem, but that was not the case.
It crashes even at this.
Can anyone suggest what is happening ?

import matplotlib.pyplot as plt 
import pandas as pd
import xlrd
import openpyxl
import os
import numpy as np
from tkinter import *
from tkinter import messagebox

root=Tk()
root.title("Tam Pepper Programming")
# size of window
root.geometry('350x150')
#Centre the Window
root.eval('tk::PlaceWindow . center')

    
def GetFile():
    from tkinter import filedialog 
    filename = filedialog.askopenfilename(initialdir = "/home/pi/Pi4_Lan/WeatherData/", 
                                          title = "Select a File", 
                                          filetypes = (("Excel files", 
                                                        "*.xlsx*"), 
                                                       ("all files", 
                                                        "*.*"))) 
    global pathTo
    pathTo=str(filename)
    print(pathTo)
    global df
    df=pd.read_excel(pathTo, 'Sheet1' )


def Last8():
    print("Last8")

def Last24():
    print("Last24")

def Last72():
    print("Last72")



def But1():
    Last8()
def But2():  
    Last24()
def But3():  
    Last72()

#QUIT program
def close_window(): 
    root.destroy()



#Place the Buttons
b1=Button(text="Last 8 readings",command=But1)#react to button press
b1.place(x=30,y=10)#Position button

b2=Button(text="Last 24 hrs",command=But2)#react to button press
b2.place(x=150,y=10)#Position button

b3=Button(text="Last 72 Hrs",command=But3)#react to button press
b3.place(x=250,y=10)#Position button

#QUIT BUTTON
bQuit=Button(text="Quit",command=close_window)
bQuit.place(x=160,y=100)

GetFile()

root.mainloop
(Mar-07-2021, 08:55 AM)TamP Wrote: [ -> ]My python script runs on Windows 10. It uses various libraries including tkinter.
Opening it in idle, it runs perfectly.
I right click on the script and associate it with python. When I double click the script it opens as a tkinter window with buttons, then a file manager opens as programmed, and I select a file, but the script then closes.
When I open the script in idle, the program continues to work when the file is selected.
I have tried both as a .py and a .pyw. This makes no difference
I include an example below. I omitted some of the def's to see if one of them was the problem, but that was not the case.
It crashes even at this.
Can anyone suggest what is happening ?

import matplotlib.pyplot as plt 
import pandas as pd
import xlrd
import openpyxl
import os
import numpy as np
from tkinter import *
from tkinter import messagebox

root=Tk()
root.title("Tam Pepper Programming")
# size of window
root.geometry('350x150')
#Centre the Window
root.eval('tk::PlaceWindow . center')

    
def GetFile():
    from tkinter import filedialog 
    filename = filedialog.askopenfilename(initialdir = "/home/pi/Pi4_Lan/WeatherData/", 
                                          title = "Select a File", 
                                          filetypes = (("Excel files", 
                                                        "*.xlsx*"), 
                                                       ("all files", 
                                                        "*.*"))) 
    global pathTo
    pathTo=str(filename)
    print(pathTo)
    global df
    df=pd.read_excel(pathTo, 'Sheet1' )


def Last8():
    print("Last8")

def Last24():
    print("Last24")

def Last72():
    print("Last72")



def But1():
    Last8()
def But2():  
    Last24()
def But3():  
    Last72()

#QUIT program
def close_window(): 
    root.destroy()



#Place the Buttons
b1=Button(text="Last 8 readings",command=But1)#react to button press
b1.place(x=30,y=10)#Position button

b2=Button(text="Last 24 hrs",command=But2)#react to button press
b2.place(x=150,y=10)#Position button

b3=Button(text="Last 72 Hrs",command=But3)#react to button press
b3.place(x=250,y=10)#Position button

#QUIT BUTTON
bQuit=Button(text="Quit",command=close_window)
bQuit.place(x=160,y=100)

GetFile()

root.mainloop
Solved
I had root.mainloop at the end instead of root.mainloop()