Date and Timestamp in python gui form - python

i am writing some code that appends to a CSV file. The application will be used in production and only has to be small and not complex, it should print a GUI that allows users to enter the date, time, and serial number
Currently i have a program that allows me to enter data into 3 boxes, two strings for date and time so i am able to put "/" and ":". I want to be able to use a button to get the date and time and insert in the two boxes, e.g. run the software, wants me to enter a date, i press a button and todays date appears (same with the timestamp), or possible to automatically fill the box with the date and time.
As i mentioned before, currently i have the code so i enter a string in "12/12/2022" and similar with timestamp just so i can get the formatting right. But here is the code.
from tkinter import *
def save_info():
date_info = date.get()
time_info = time.get()
serialNumber_info = serialNumber.get()
serialNumber_info = str(serialNumber_info)
print(date_info, time_info, serialNumber_info)
file = open("test.csv", "a")
file.write(date_info)
file.write(",")
file.write(time_info)
file.write(",")
file.write(serialNumber_info)
file.write("\n")
file.close()
print(" User ", date_info, " Has been registered")
date_entry.delete(0, END)
time_entry.delete(0, END)
serialNumber_entry.delete(0, END)
screen = Tk()
screen.geometry("500x500")
screen.title("Python Form")
heading = Label(text = "Python Form", bg = "grey", fg = "black", width = "500", height = "3")
heading.pack()
date_text = Label(text = "Date * ",)
time_text = Label(text = "Time * ",)
serialNumber_text = Label(text = "Serial Number * ",)
date_text.place(x = 15, y = 70)
time_text.place(x = 15, y = 140)
serialNumber_text.place(x = 15, y = 210)
date = StringVar()
time = StringVar()
serialNumber = IntVar()
date_entry = Entry(textvariable = date, width = "30")
time_entry = Entry(textvariable = time, width = "30")
serialNumber_entry = Entry(textvariable = serialNumber, width = "30")
date_entry.place(x = 15, y = 100)
time_entry.place(x = 15, y = 180)
serialNumber_entry.place(x = 15, y = 240)
register = Button(screen,text = "Register", width = "30", height = "2", command = save_info, bg = "grey")
register.place(x = 15, y = 400)

You can use datetime module to get the current time to fill the date and time entry boxes:
...
from datetime import datetime
...
def fill_datetime():
# get current date and time
now = datetime.now()
# fill date
date.set(now.strftime("%d/%m/%Y"))
# fill time
time.set(now.strftime("%T"))
datetime_button = Button(screen, text="Fill Date and Time", width=30, height=2, command=fill_datetime, bg="grey")
datetime_button.place(x=15, y=300)
...

Related

Preloading Entry tkinter with Date

I am writing a program, that users enter information in which the data is then outputted into a CSV once clicked generate.
The info that needs to be entered is: Date, Time, Test Station, Serial Number, and then 3 radio button selections.
Currently, i have it so i can enter the date, time, and serial number. But i want the application to automatically update the date and time so that users don't have to enter it. I have imported a date module but no idea how to get it working within the text box.
from tkinter import *
from datetime import date
today = date.today()
d1 = today.strftime("%d/%m/%y")
def save_info():
date_info = date.get()
time_info = time.get()
serialNumber_info = serialNumber.get()
serialNumber_info = str(serialNumber_info)
print(date_info, time_info, serialNumber_info)
file = open("test.csv", "a")
file.write(date_info)
file.write(",")
file.write(time_info)
file.write(",")
file.write(serialNumber_info)
file.write("\n")
file.close()
print(" User ", date_info, " Has been registered")
date_entry.delete(0, END)
time_entry.delete(0, END)
serialNumber_entry.delete(0, END)
screen = Tk()
d1_var = StringVar(screen, d1)
screen.geometry("500x500")
screen.title("Python Form")
heading = Label(text = "Python Form", bg = "grey", fg = "black", width = "500", height = "3")
heading.pack()
date_text = Label(text = "Enter Date '(13/12/2022)' ",)
time_text = Label(text = "Enter Time '(16:45)'",)
serialNumber_text = Label(text = "Enter Serial Number ",)
date_text.place(x = 15, y = 70)
time_text.place(x = 210, y = 70)
serialNumber_text.place(x = 15, y = 210)
date = StringVar()
time = StringVar()
serialNumber = IntVar()
date_entry = Entry(textvariable = d1_var, width = "30")
time_entry = Entry(textvariable = time, width = "30")
serialNumber_entry = Entry(textvariable = serialNumber, width = "30")
date_entry.place(x = 15, y = 100)
time_entry.place(x = 210, y = 100)
serialNumber_entry.place(x = 15, y = 240)
register = Button(screen,text = "Register", width = "30", height = "2", command = save_info, bg = "grey")
register.place(x = 15, y = 400)
the text variable you define to your Entry widget should be a tkinter StringVar. You can define it after creating the Tk() and set any value you want.
screen = Tk()
d1_var = StringVar(screen, d1)
Then just change the Entry textvariable to d1_var instead:
date_entry = Entry(textvariable = d1_var, width = "30")

How do I make this python GUI run this executable?

I'm trying to make a basic gui menu option that will run my stated exe (hurl), but it won't. Here is my code:
from tkinter import *
import os
root = Tk()
root.geometry('350x150')
root.title("hurler")
photo = PhotoImage(file = "Logo_Image.png")
root.iconphoto(False, photo)
entry_text = Label(text = "Number of posts you wish to automate (between 1-12) * ")
entry_text.place(x = 15, y = 10)
num = StringVar()
time_entry = Entry(textvariable = num, width = "10")
time_entry.place(x = 15, y = 40)
def action():
if num == '1':
os.system(".\hurl\hurl.exe")
register = Button(root,text = "Make", width = "10", height = "2", command = action, bg = "lightblue")
register.place(x = 15, y = 70)
root.mainloop()
I'm not the most experienced at this, so any feedback helps.
How do I get option 1 to run this exe? Thanks.
so these are the improvements, should work now:
from tkinter import *
import os
root = Tk()
root.geometry('350x150')
root.title("hurler")
# photo = PhotoImage(file = "Logo_Image.png")
# root.iconphoto(False, photo)
entry_text = Label(text = "Number of posts you wish to automate (between 1-12) * ")
entry_text.place(x = 15, y = 10)
num = StringVar()
time_entry = Entry(textvariable = num, width = "10")
time_entry.place(x = 15, y = 40)
def action():
global num
num = num.get()
if num == '1':
os.startfile('https://www.google.com')
num = StringVar()
register = Button(root,text = "Make", width = "10", height = "2", command = action, bg = "lightblue")
register.place(x = 15, y = 70)
root.mainloop()
the changes were made here:
def action():
global num
num = num.get()
if num == '1':
os.startfile('https://www.google.com')
num = StringVar()
also should work with the os.system thingy

GUI doesnt open for Entry string

I keep getting the error time "data 'startdate_info' does not match format '%Y-%m-%d'" but it doesn't open the GUI for the data Entries. It should open a GUI for start date, end date, ticker and inspect string entry. Those strings should go into:
newtime = yf.download('ticker_info', start = 'startdate_info', end = 'enddate_info')
But for some reason I keep getting stuck on this line 55.
import yfinance as yf
import pandas as pd
import pyautogui
from tkinter import *
import keyboard
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import pyplot
screen = Tk()
screen.geometry("450x550")
startdate = StringVar()
start_date = Label(text = "Enter start date in YYYY-MM-DD format: ")
start_date.place(x = 15, y = 70)
startdate_entry = Entry(textvariable = startdate, width = "30")
startdate_entry.place(x = 15, y = 120)
enddate = StringVar()
end_date = Label(text = "Enter end date in YYYY-MM-DD format: ")
end_date.place(x = 15, y = 170)
enddate_entry = Entry(textvariable = enddate, width = "30")
enddate_entry.place(x = 15, y = 220)
tickerE = StringVar()
ticker_label = Label(text = "Ticker symbol: ")
ticker_label.place(x = 15, y = 270)
ticker = Entry(textvariable = tickerE, width = "30")
ticker.place(x = 15, y = 320)
inspectE = StringVar()
inspect_label = Label(text = "What would you like to analyze ")
inspect_label.place(x = 15, y = 270)
inspect_label = Label(text = "(Open, High, Low, Close, Adj Close, Volume)? ")
inspect_label.place(x = 15, y = 320)
inspect = Entry(textvariable = inspectE, width = "30")
inspect.place(x = 15, y = 370)
def save_info():
startdate_info = startdate.get()
enddate_info = enddate.get()
ticker_info = tickerE.get()
inspect_info = inspectE.get()
return ticker_info, inspect_info, startdate_info, enddate_info
save_info()
search = Button(screen,text = "Search", width = "30", height = "2", command = save_info, bg = "grey")
search.place(x = 14, y = 410)
ticker_info, inspect_info, startdate_info, enddate_info = save_info()
newtime = yf.download('ticker_info', start = 'startdate_info', end = 'enddate_info')
print(newtime)
def adjusted_close(ticker_info, newtime):
newtime[inspect_info].plot()
plt.xlabel("Date")
plt.ylabel(inspect_info)
plt.title(ticker_info + " " + inspect_info + " " + "Data")
plt.show()
adjusted_close(ticker_info, newtime)
You passed strings to yf.download():
newtime = yf.download('ticker_info', start = 'startdate_info', end = 'enddate_info')
You should pass the variables:
newtime = yf.download(ticker_info, start=startdate_info, end=enddate_info)
Also you should not executed the function just after creating the Entry widgets because there is nothing input yet. You should execute the function inside save_info():
def save_info():
startdate_info = startdate.get()
enddate_info = enddate.get()
ticker_info = tickerE.get()
inspect_info = inspectE.get()
print(startdate_info, enddate_info, ticker_info, inspect_info, sep=",")
newtime = yf.download(ticker_info, start=startdate_info, end=enddate_info)
print(newtime)
Below is a cut-down example based on your code:
from tkinter import *
import yfinance as yf
import matplotlib.pyplot as plt
screen = Tk()
screen.geometry("450x580")
startdate = StringVar()
start_date = Label(text="Enter start date in YYYY-MM-DD format:")
start_date.place(x=15, y=70)
startdate_entry = Entry(textvariable=startdate, width=30)
startdate_entry.place(x=15, y=120)
enddate = StringVar()
end_date = Label(text="Enter end date in YYYY-MM-DD format:")
end_date.place(x=15, y=170)
enddate_entry = Entry(textvariable=enddate, width=30)
enddate_entry.place(x=15, y=220)
tickerE = StringVar()
ticker_label = Label(text="Ticker symbol:")
ticker_label.place(x=15, y=270)
ticker = Entry(textvariable=tickerE, width=30)
ticker.place(x=15, y=320)
inspectE = StringVar()
inspect_label = Label(text="What would you like to analyze")
inspect_label.place(x=15, y=370)
inspect_label = Label(text="(Open, High, Low, Close, Adj Close, Volume)?")
inspect_label.place(x=15, y=420)
inspect = Entry(textvariable=inspectE, width=30)
inspect.place(x=15, y=470)
def save_info():
startdate_info = startdate.get()
enddate_info = enddate.get()
ticker_info = tickerE.get()
inspect_info = inspectE.get()
print(startdate_info, enddate_info, ticker_info, sep=",")
newtime = yf.download(ticker_info, start=startdate_info, end=enddate_info)
print(newtime)
# plot it
newtime[inspect_info].plot()
plt.xlabel("Date")
plt.ylabel(inspect_info)
plt.title(f"{ticker_info} {inspect_info} Data")
plt.show()
search = Button(screen, text="Search", width="30", height="2", command=save_info, bg="grey")
search.place(x=14, y=510)
screen.mainloop()

py2exe not working - flashing console and closes

EXE won't open. I have tried everything I found online.
When I run the exe the console opens for a few seconds and then closes.
How can I make this .py executable and have my .txt output?
from tkinter import *
import time, os, fnmatch, shutil
screen = Tk()
screen.geometry("600x450")
screen.configure(background='azure')
screen.title("M")
t = time.localtime()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
BACKUP_NAME = ("backup-" + timestamp)
heading = Label(text = "Mr", font=(None, 15), bg = "lightblue", fg = "black", width = "500", height = "3")
heading.pack()
firstname_text = Label(text = "Name/ * ", bg = "azure",)
lastname_text = Label(text = "Usagel* ", bg = "azure",)
material_text = Label(text = "M * ", bg = "azure",)
age_text = Label(text = "How * ", bg = "azure",)
firstname_text.place(x = 15, y = 80)
lastname_text.place(x = 15, y = 140)
material_text.place(x = 15, y = 210)
age_text.place(x = 15, y = 260)
firstname = StringVar()
lastname = StringVar()
material = StringVar()
age = StringVar()
firstname_entry = Entry(textvariable = firstname, width = "30")
lastname_entry = Entry(textvariable = lastname, width = "30")
material_entry = Entry(textvariable = material, width = "30")
age_entry = Entry(textvariable = age, width = "30")
firstname_entry.place(x = 15, y = 100)
lastname_entry.place(x = 15, y = 170)
material_entry.place(x = 15, y = 240)
age_entry.place(x = 15, y = 290)
register = Button(screen,text = "Submit", font=(None, 10), width = "30", height = "2", command = save_info, bg = "lime green")
register.place(x = 15, y = 320)

Is it possible to grab a label variable and put into list with tkinter

I've created a temperature converter programme in which the calculated temperature from an entry widget gets displayed in a separate label, what I need to do is to grab that converted variable and put it into a list.
I think that making a connected entry widget to the label widget would work where they are connected so I could grab the variable using the .get method but that would look awfully messy. Is there any other way I could proceed with this?
This is my first post and I am a beginner in Python, very sorry if the code looks messy and if I included too much code.
data = []
tempVal = "Celcius"
def store_temp(sel_temp):
global tempVal
tempVal = sel_temp
class Calculator:
def __init__(self, num_a, num_b):
self.num_a= num_a
self.num_b = num_b
def convert(self):
if tempVal == 'Fahrenheit':
return float((float(self.num_a) - 32)* 5 / 9)
if tempVal == 'Celcius':
return float((float(self.num_a) * 9/ 5) + 32)
def display_add(entry_numa,entry_numb,label_answer):
#get the value from entry_numa
num_a = entry_numa.get()
num_b = entry_numb.get()
num_a = str(num_a)
num_b = str(num_b)
#create an object
global data
calc = Calculator(num_a,num_b)
label_answer['text'] = calc.convert()
data += [calc]
def calc_history():
global data
#creat e another window
window_calc_list = Tk()
window_calc_list.geometry("400x200")
#create a listbox
listbox_calc_list = Listbox(window_calc_list, width= 300)
listbox_calc_list.pack()
listbox_calc_list.insert(END, "list of data")
for info in data:
listbox_calc_list.insert(END, str(info.num_a) + " " + str(info.num_b) + " " )
window_calc_list.mainloop()
def main():
window = Tk()
window.geometry("500x150")
validate_letter = window.register(only_letters)
validate_nb = window.register(only_numbers_max_3)
label = Label(window, width = 30, background = 'lightblue', text='enter temperature, only numbers')
label.grid(row=0, column=0)
entry_numa = Entry(window, width = 30, validate="key", validatecommand=(validate_nb, '%d', '%P'))
entry_numa.grid(row = 0, column = 1)
#create another label and entry object for num_b
label_numb = Label(window, width = 30, background = 'lightblue', text='enter location, only letters')
label_numb.grid(row=1, column=0)
entry_numb = Entry(window, width = 30, validate="key", validatecommand=(validate_letter, '%d', '%S'))
entry_numb.grid(row = 1, column = 1)
#create another label to display answer
label_answer = Label(window, width = 30, background = 'lightyellow')
label_answer.grid(row = 2, column = 1)
entry_answer = Entry(window, width = 30)
entry_answer.grid(row = 2, column = 0)
button_add = Button(window, text = "ADD", command = lambda: display_add(entry_numa,entry_numb,label_answer))
button_add.grid(row=3, column = 0)
button_delete = Button(window, text = "DELETE", command = lambda: delete_data(data))
button_delete.grid(row=3, column = 2)
#create another button to display all previous calculations
button_display = Button(window,text = "calc_history", command = lambda: calc_history())
button_display.grid(row=3, column = 1)
var = StringVar()
dropDownList = ["Celcius", "Fahrenheit"]
dropdown = OptionMenu(window, var,dropDownList[0], *dropDownList, command=store_temp)
dropdown.grid(row=0, column=2)
window.mainloop()
A tk.Label displayed value can be accessed via the text property
, labelwidgetname['text'].
Depeneding on when and how you want the independent list of stored values
to be updated there are a variety of options. The example shows one if the
user is required to press a submission button. This could be adapted,
for example,when the tempreture calculation is performed.
Of course it would be simpler to update the list of stored values directly at the point in the script where the calculated tempreture for the label text has been derived.
import tkinter as tk
stored_values = []
def add_labelvalue_tolist(temp):
'''Store label value to list.'''
stored_values.append(temp)
print('contents of list', stored_values)
def add_entry_tolabel(event):
display_label['text'] = user_entry.get()
ROOT = tk.Tk()
user_entry = tk.Entry()
user_entry.grid(column=0, row=0)
user_entry.bind('<KeyRelease>', add_entry_tolabel)
display_label = tk.Label()
display_label.grid(column=1, row=0)
# update list via button command linked to label text value
add_button = \
tk.Button(text='add to list',
command=lambda:add_labelvalue_tolist(display_label['text']))
add_button.grid(column=0, row=1)
ROOT.mainloop()
try making a function that is like this
def letterused():
converter=(letter.get())# letter is a entry box at the bottom is the code
converted.set(converter)
for i in range(1):
used_letters1.append(converter) #list
letter = ttk.Entry(root, width = 20,textvariable = letter)
letter.pack()

Categories

Resources