Python Forum
Gui -> Dateime -> grid - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Gui -> Dateime -> grid (/thread-15487.html)



Gui -> Dateime -> grid - pynewby - Jan-19-2019

Hello all:

I've spent a better part of two days before coming here..
I'm just trying to put the current date in a .grid

I'm wondering if there is an easier way to do this.

import tkinter
import datetime
from tkinter import *
from tkinter import Tk

mainWindow: Tk = tkinter.Tk()
mainWindow.title("Camera Control Interface")
w = 800 # Screen Width
h = 700 # Screen Height

# What is the machines screen width and height
ws = mainWindow.winfo_screenwidth()
hs = mainWindow.winfo_screenheight()

# Calculate screen positioning for mainWindow
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

# Set dimensions of screen from W and H above
# And where is screen placed
mainWindow.geometry('%dx%d+%d+%d' % (w, h,x, y))

# Define date time
now = datetime.datetime.now()
today = now.date()
today.strftime('%m/%d/%Y')
moment = now.time()
moment.strftime("%H:%M:%S") # 24 hour time - can change to 12 hour with a %I

# Start of configuration inputs here name, date, time, etc. . .
label_1 = Label(mainWindow, text="Confirm Date")
label_1.grid(row=0, column=0)
label_2 = Label(mainWindow, print(today.strftime('%m/%d/%Y')))
label_2.grid(row=0, Column=1)

mainWindow.mainloop()
The output of the date is create but the gui is having issues..

Error output ->
01/19/2019
Error:
Traceback (most recent call last): File "C:/Users/tracy/Documents/Camera Project PY/cpgui.py", line 34, in <module> label_2 = Label(mainWindow, print(today.strftime('%m/%d/%Y'))) File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2766, in __init__ Widget.__init__(self, master, 'label', cnf, kw) File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2292, in __init__ BaseWidget._setup(self, master, cnf) File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2264, in _setup if 'name' in cnf: TypeError: argument of type 'NoneType' is not iterable
Process finished with exit code 1

***

I've learned quite a bit over the last couple of days reading about datetime..
so I don't think I'm doing too bad for starting with no knowledge.. :)

Anyways if there is an easier way to program a gui / grid I'm open to suggestions
or if there is an easier way to do what I'm trying to do here?

Looks like the date is getting computed just find. just has issues when trying to
get it to grid. Just trying to get current date to show in grid.

Since the date is coming up. I'm guessing the issue is at line 33 for label_2

Any suggests for a newb would be cool.. :)
thank you


RE: Gui -> Dateime -> grid - Larz60+ - Jan-19-2019

you can't print into a label (line 33), you must assign text line in line 31
change line 33 from:
label_2 = Label(mainWindow, print(today.strftime('%m/%d/%Y')))
to:
label_2date = today.strftime('%m/%d/%Y')
label_2 = Label(mainWindow, text=label2date)
this only works once, because the second time, you can't create another label2, you need to reuse existing one
to do that you need a tkinter StringVal, set to the date, and in the label use textvariable = StringVal name.


RE: Gui -> Dateime -> grid - pynewby - Jan-24-2019

thank you..

I inserted code and fixed a few typos of my own..

it works

Thanks again..