I have an issue with this tkinter dropdown window. When running it by itself the code runs great, but when I run it as part of a larger code, the script gets stuck at this step unless I restart the kernel before every run. As long as I restart the kernel before running the script, the code works. How can I fix this issue and have the script run without having to restart the kernel before every run?
from tkinter import *
OPTIONS = [
"Physician 1",
"Physician 2",
"Physician 3"
]
master = Tk()
master.title("Physician Name")
master.geometry('300x200')
master['bg'] = '#add8e6'
variable = StringVar(master)
variable.set(OPTIONS[0]) # default value
w = OptionMenu(master, variable, *OPTIONS)
w.pack()
def ok():
global name
name =variable.get()
print (name)
master.destroy()
button = Button(master, text="OK", command=ok)
button.pack()
mainloop()
Update:
I use tkinter to choose file:
# GUI.py
from tkinter.filedialog import askopenfilename
import tkinter as tk
# Create the window and hide it
root = tk.Tk()
root.withdraw()
# Now you are free to popup any dialog that you need
filetypes = (("PDF file", "*.pdf"), ("All files", "*.*"))
filepath = askopenfilename(filetypes=filetypes)
# Now use the filepath
import re
import pdfplumber
import pandas as pd
from collections import namedtuple
lines = []
with pdfplumber.open(filepath) as pdf:
pages = pdf.pages
for page in pdf.pages:
text = page.extract_text()
# print(text)
# Destroy the window
root.destroy()
Then I have the code posted above where I have a dropdown menu
And finally I use this code to prompt the user to choose the name of the final file:
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
pdfPath = filedialog.asksaveasfilename(defaultextension = "*.pdf", filetypes = (("PDF Files", "*.pdf"),))
if pdfPath: #If the user didn't close the dialog window
pdfOutputFile = open(pdfPath, 'wb')
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()
pdf1File.close()
pdf2File.close()