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")
Related
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)
...
from tkinter import *
import time
check = False
window = Tk()
window.geometry("1920x1080")
def typeTime():
hour = int(time.strftime("%H"))
minute = int(time.strftime("%M"))
second = int(time.strftime("%S"))
hourInput2 = int(hourInput.get())
minuteInput2 = int(minuteInput.get())
secondInput2 = int(secondInput.get())
if(hour == hourInput2 and minute == minuteInput2 and second == secondInput2):
print("now")
global check
check = True
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20)).place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20)).place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20)).place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10), command = typeTime)
submit.place(x = 100, y = 100)
if check == True:
print("Pressed")
submit.config(relief = SUNKEN)
window.mainloop()
I'm trying to make a button to stay pressed, so I tried to make this happens with a global variable. The variable check is initially False, but when typeTime() is called via the submit object it should change its value in True and when check will be tested later to keep my button pressed using config method.
What am I doing wrong, as neither the button is still pressed nor the message "Pressed" is displayed in the console ?
The window.mainloop() is the internal loop inside object window, not in your script so that is why it didn't work. You need to add the action inside the function typeTime:
from tkinter import *
import time
if __name__=='__main__':
check = False
window = Tk()
window.geometry("1920x1080")
def typeTime(button):
hour = int(time.strftime("%H"))
minute = int(time.strftime("%M"))
second = int(time.strftime("%S"))
hourInput2 = int(hourInput.get())
minuteInput2 = int(minuteInput.get())
secondInput2 = int(secondInput.get())
if(hour == hourInput2 and minute == minuteInput2 and second == secondInput2):
print("now")
# global check
# check = True
print('Pressed')
button.config(relief=SUNKEN)
canvas = Canvas(window, width = 1980, height = 1020)
canvas.pack()
hourInput = StringVar()
minuteInput = StringVar()
secondInput = StringVar()
setHour = Entry(window, text = hourInput, font = (20)).place(x = 100, y = 20, width = 100, height = 40)
setMinute = Entry(window, text = minuteInput, font = (20)).place(x = 300, y = 20, width = 100, height = 40)
setSecond = Entry(window, text = secondInput, font = (20)).place(x = 500, y = 20, width = 100, height = 40)
canvas.create_text(60, 40, text = "Hour: ", font = (20))
canvas.create_text(260, 40, text = "Minute: ", font = (20))
canvas.create_text(460, 40, text = "Second: ", font = (20))
submit = Button(text = "Submit", height = 2, width = 10, font = (10))
submit.config(command = lambda submit=submit:typeTime(submit))
submit.place(x = 100, y = 100)
# if check == True:
# print("Pressed")
# submit.config(relief = SUNKEN)
window.mainloop()
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
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()
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)