I've tried a variety of things but can't seem to figure this out.
Here's a general overview of my program, which takes an Excel file as an import and outputs a .pdf of charts:
Ask user to enter a file name for the outputted file (using Tkinter textbox input)
Ask user to select the input file (using askopenfilename)
Does a bunch of work with Pandas to analyze data, then plots it with matplotlib and PdfPages
Displays a message with Tkinter that says the file was outputted successfully, along with a "Exit" button, which should end the program, but does not.
Here's #1 (separately, I'm wondering how I could modify this code to accept the "Enter" button instead of having to physically click the button):
root = tk.Tk()
root.title("Enter a file name for the report")
def getOutputFileName():
global outputFileName
outputFileName = textBox.get("1.0",'end-1c')
print(outputFileName)
root.quit()
textBox = Text(root, height=1, width = 25)
textBox.pack()
buttonCommit = Button(root, height=5, width=50, text="After entering a file name, click here.", command=lambda: getOutputFileName())
buttonCommit.pack()
root.mainloop()
root.withdraw()
Here's #2:
filetypes=[("Excel files", ".xlsx .xls")]
file_path = fd.askopenfilename(filetypes=filetypes, title = 'Open the Excel input file')
df = pd.read_excel(file_path) ## Start of Pandas code
#3 shouldn't be relevant as it doesn't use Tkinter whatsoever (plus the .pdf is exporting correctly), so here's #4:
window = tk.Toplevel() ## Here I tried using root = tk.Tk() but learned that Toplevel() needs to be used for windows after the first?
window.Title = "Success!"
window.geometry("250x170")
T = Text(window, height = 5, width = 52)
successText = "Report successfully generated. The .pdf file was saved to the same location from which you ran this program. You may close the program now."
b1 = Button(window, text = "Exit", command = window.destroy) ## This button DOES close the window, but doesn't close the entire program (i.e. my IDE stays running the script until I force stop)
T.pack()
b1.pack()
T.insert(tk.END, successText)
tk.mainloop()
print("End of program.") ## this is for testing - the program never gets here.
Any advice would be amazing. Thank you.
Related
Tkinter is adding newlines every 30 seconds:
Unwanted newlines
I'm running this in windows CMD.
I wrote a simple program to test this with a single askopenfilename() call in there.
When I click the browse button, it adds a newline, then after I choose the file and click begin, it adds a newline every 30 seconds while the program is "running":
import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import askdirectory
import time
def input1():
input1_path = tk.filedialog.askopenfilename()
input1_entry.delete(1, tk.END) # Remove current text in entry
input1_entry.insert(0, input1_path) # Insert the 'path'
#returns file paths for the input files and output directory
def begin():
global inputFileName
inputFileName = input1_entry.get()
master.destroy()
master = tk.Tk()
master.title('Omega NExT Archive Plotter')
one_frame = tk.Frame(master)
two_frame = tk.Frame(master)
line1 = tk.Frame(master, height=1, width=400, bg="grey80", relief='groove')
input1_path = tk.Label(one_frame, text="Archive File Input:")
input1_entry = tk.Entry(one_frame, text="", width=60)
browse1 = tk.Button(one_frame, text="Browse", command=input1)
begin_button = tk.Button(two_frame, text='Begin!', command=begin)
one_frame.pack(side=tk.TOP)
line1.pack(pady=10)
two_frame.pack(side=tk.BOTTOM)
input1_path.pack()
input1_entry.pack()
browse1.pack(pady=10)
begin_button.pack(pady=20, fill=tk.X)
master.mainloop()
time.sleep(1000000)
So I have other guys I work with running this on their computers, and it sounds like the problem is happening on our "managed" computers, but not the "less-than-managed" computers. It sounds like some kind of weird IT issue.
I have created a small GUI program using tkinter (Python 3.8.8) however when I create an .exe file using this command in the cmd : pyinstaller --onefile myprogram.py. It does create the executable file however when I click this .exe file, it close the program as soon as it start to executes. To resolve that issue I have tried the following:
I have put Input("Enter any key to end") in the end of the code.
I have run the command without using --onefile in CMD.
It does not show any error in the console. When I run my code in spyder IDE, It does not cause any issue.
Please guide me how do I configure that .exe file to not to close the program.
def click():
df = pd.read_csv("meta.csv")
State = df["State"]
# Ask for the directory where you want to save the file
paths = askdirectory(title='Select Folder')
print(paths)
dt = datetime.now()
# Mapping of TextBox
entered_text = txtSurName.get()
# Final file name format will be like: DateTime+Surname.csv
dt_res = dt.strftime("%d-%b-%Y%H%M%S%f")
dt_res = "/"+ dt_res + entered_text +".csv"
dic = {'state' : State}
df = pd.DataFrame(dic)
df.to_csv(paths+dt_res)
window = Tk()
window.configure(background="black")
# Labels and their value assigment
Label(window, text="Enter Surname ", bg="black",fg="white", font="none 12 bold").grid(row=1,column=0,sticky=W);
txtSurName = Entry(window, width=50, bg="white");
txtSurName.grid(row=2, column=0, sticky=W);
# Button
Button(window, text="Submit" , width = 6, command=click).grid(row=6,column=0,sticky=W);
#GUI LOGIC
window.mainloop()
input('Press ENTER to exit')
I am trying to solve a problem that seems simple, but I cannot figure out how.
I want to create a simple program checking if a certain symbol exists in a text file:
The program starts;
The user clicks a button (inside the window, not in the menu);
A dialog box appears;
The user chooses a text file;
A message box displays the result;
The program closes.
Pretty straightforward, but I cannot find how to save the filename into a variable and then use it for the process. I read so many tutorials and I could not find a solution. Here is the code:
from tkinter import *
from tkinter import filedialog
def clicked():
global filename
filename = filedialog.askopenfile(filetypes=(("Word files","*.docx"),))
window = Tk()
window.geometry()
window.title("My App")
open_file_label = Label(window, text="Open your docx file here:", font=("Arial", 10), padx=5, pady=5)
open_file_label.grid(column=0, row=0)
open_file_button = Button(window, text="Click me", command=clicked, padx=5, pady=5)
open_file_button.grid(column=1, row=0)
window.mainloop()
filename is already a variable that contains the chosen file's contents. Just print(filename) and you'll get your data printed in console.
GUI consists one Run Button which should run the first program as many times user wants & display output in tkinter text.
My code(2nd .py file):
from tkinter import*
from tkinter import ttk
import Random Game
root = Tk()
root.title("Random Game 1.0")
quote = "Long \nRandom \nText \nGenerated \nBy \nRandom Function \nAnd \nControl Structures"
frame = Frame(root)
labelText = StringVar()
label = Label(frame, textvariable=labelText).pack()
button = Button(frame, text="Click to Run").pack()
labelText.set("Random Game 1.0")
Label(root, text ="")
T = Text(root)
frame.pack()
T.pack()
T.insert(END, quote)
root.mainloop()
I want Output of 1st program which is random every time in "tkinter text of another (2nd program) instead of quote lines mentioned in above code.
Output first program to txt file.
Read from that txt file into the tkinter GUI by checking when it was last modified so as to avoid mutex issues.
Therefore:
# Prog 1:
file = open("log.txt", "w")
# code that writes there
# Prog 2:
file = open("log.txt", "r")
# use data to show in the tkinter with its mainloop for example
# in mainloop()...
# .....
if other_prog_has_written_something_new :
data = file.readlines()
useDataInGUI(data)
quick question here. Is there a really easy way to show a user a message in like a text box in python? I just need to input a string beforehand so I can show the message after a loop runs. Thanks!
EDIT: Just Windows 8.1 and straight python 2.7
If I'm correct that you want a window to let a user type in some text, then show it back as a message box, then you need two modules, tkMessageBox and Tinker, two standard modules in Python 2.7+.
The following documented code does the above
from Tkinter import *
import tkMessageBox
def getInfo():
#This function gets the entry of the text area and shows it to the user in a message box
tkMessageBox.showinfo("User Input", E1.get())
def quit():
#This function closes the root window
root.destroy()
root = Tk() #specify the root window
label1 = Label( root, text="User Input") #specify the label to show the user what the text area is about
E1 = Entry(root, bd =5) #specify the input text area
submit = Button(root, text ="Submit", command = getInfo) #specify the "submit" button that runs "getInfo" when clicked
quit_button = Button(root, text = 'Quit', command=quit) #specify the quit button that quits the main window when clicked
#Add the items we specified to the window
label1.pack()
E1.pack()
submit.pack(side =BOTTOM)
quit_button.pack(side =BOTTOM)
root.mainloop() #Run the "loop" that shows the windows