Home > database >  Read in a file with tkinter and continue with following code
Read in a file with tkinter and continue with following code

Time:09-20

import tkinter as tk
from tkinter import *   # from fileinput import fileame
from tkinter import filedialog
from tkinter import messagebox

window = tk.Tk()  # Definition Fenster mit tkinter (TK)
window.title('Testautomatisierung_Tool')
# root.attributes('-fullscreen', True)
window.geometry('840x450 200 100') # Länge x Breite

def browse_testfall():
    file_testfall = filedialog.askopenfilename(initialdir="C:/Users/graethem/PycharmProject/Testautomatisierung_Tool_V1/V1", title="Select a Excel-File for processing", filetypes=(("Microsoft-Excel-Arbeitsblatt",                                          "*.xlsx*"), ("all files", "*.*")))

    label_file_explorer_testfall.configure(text="File Opened: "   file_testfall)
    return file_testfall

def exit():  
    if messagebox.askquestion("Exit", "Do you want to Exit?"):
        window.destroy()


dlg = filedialog.Open(window, initialdir="C:/Users/V1",
                      title="Select a Excel-File for processing",
                      filetyps=(('Python files', '*.py'), ('All files', '*')))

labelTitel = Label(master=window, text='Dies ist ein Tool für die generische Testfallerstellung. Der Testfall aus einer Excel-Datei wird in ein in CANoe ausführbares Test-Template geschrieben.')
labelTitel.place(x=15, y=15, height=30)

labelTitel = Label(master=window, text='Bitte wählen Sie ein .xlsx File welches in einen Testfall umgewandelt werden soll!')
labelTitel.place(x=15, y=40, height=30)

button_explore_testfall = Button(window, text="Browse Files", command=browse_testfall)
button_explore_testfall.place(x=190, y=65, height=30)

label_file_explorer_testfall = Label(window, text="File Opened:")
label_file_explorer_testfall.place(x=15, y=95, height=30)


button_exit = Button(window, text="Exit", command=exit)
button_exit.place(x=500, y=500, height=30)

window.mainloop()

###

# code after loop

I want to read in the Excel file and when it is done I want to press the exit button and the code after the mainloop should be executed. I already tried update_idletasks() but this also doesn't work. So how do I exit the window and the loop after pressing the exit button?

CodePudding user response:

Change the location of the button so it is in view:

Example:


button_exit.place(x=50, y=50, height=30)

Then it responds as it should in my setup.

CodePudding user response:

The output of messagebox.askquestion is not boolean, but rather a string containing 'yes' or 'no'. So you need to test the string value. The extra code in case of 'no' should go into a function. Nothing stops you from calling that function after a yes aswell. With a small change to exit and adding a function 'extracode' you can make this all work:


def extracode():
    print('Die Gedanken sind frei')

def exit():  
    bExit =  messagebox.askquestion("Exit", "Do you want to Exit?")
    print(bExit)
    if bExit == 'yes':
        window.destroy()
    else: 
        extracode()
  • Related