How do I get the Value of Tkcalendar/Date Entry - python

hi I am building a GUI where it should be possible to insert your name and your brthday that get´s stored in a sqllite3 database.
I use the Tkcalendar module to let the person choose it´s birthday.
My problem is I don´t know how to read out the Date.
I hope somebody can help me!
import sqlite3
verbindung = sqlite3.connect("Datenbanken/geb3.db")
zeiger = verbindung.cursor()
zeiger.execute("""CREATE TABLE IF NOT EXISTS people3(vorname VARCHAR(20), nachname VARCHAR(30), geburtstag DATE );""")
from tkinter import *
from tkcalendar import Calendar, DateEntry
root = Tk()
def speichern():
zeiger.execute("""INSERT INTO people3 VALUES (?,?,?) """, (eingabefeldvorname.get(), eingabefeldnachname.get(), eingabefelddatum.get()))
Label(root, text = "vorname").pack()
eingabefeldvorname=StringVar()
eingabefeld = Entry(root, textvariable = eingabefeldvorname, bg ="red").pack()
Label(root, text = "nachname").pack()
eingabefeldnachname = StringVar()
eingabefeld = Entry(root, textvariable = eingabefeldnachname, bg ="green").pack()
Label(root, text = "Geburtsdatum")
eingabefelddatum= DateEntry(root, width = 20, textvariable = eingabefelddatum, bg ="yellow").pack()
konpf2 = Button(root, text = "speichern", command = speichern).pack()
root.mainloop()
zeiger.execute("SELECT * FROM people3")
inhalt = zeiger.fetchall()
print(inhalt)
def listeleeren():
zeiger.execute("DELETE FROM people3")
listeleeren()
verbindung.commit()
verbindung.close()```

To get the value of DataEntry, the fuction get_date() works.
Reference: https://pypi.org/project/tkcalendar/#dateentry-widget
also you can use a <<DateEntrySelected>> event which is generated each time the user selects a date with the mouse.
import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar, DateEntry
def dateentry_view():
def print_sel(e):
print(cal.get_date())
top = tk.Toplevel(root)
ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
cal = DateEntry(top, width=12, background='darkblue',
foreground='white', borderwidth=2)
cal.pack(padx=10, pady=10)
cal.bind("<<DateEntrySelected>>", print_sel)
root = tk.Tk()
ttk.Button(root, text='DateEntry', command=dateentry_view).pack(padx=10, pady=10)
root.mainloop()

Related

Issue with refreshing values in a Tkinter window

Here is the code:
import steammarket as sm
import time
from tkinter import *
from PIL import ImageTk, Image
#Search function which gets the request from the market
name = ('Operation Broken Fang Case')
item = sm.get_csgo_item(name, currency='GBP')
#GUI
window = Tk()
window.title("OSMA")
window.geometry("300x110")
Lbl = Label(window, text=name, font= ('Helvetica 9 underline'))
Lbl.grid(column=0, row=0)
#Image for case
image1 = Image.open("FangCase.png")
case1 = ImageTk.PhotoImage(image1)
label1 = Label(image=case1)
label1.image = case1
label1.place(x=20, y=20)
Lbl = Label(window, text='Average cost: '+item["median_price"])
Lbl.grid(column=1, row=3)
Lbl = Label(window, text='Lowest cost: '+item["lowest_price"])
Lbl.grid(column=1, row=4)
Lbl = Label(window, text='Listings: '+item["volume"])
Lbl.grid(column=1, row=5)
#Waits 10 mins, searches again and refreshes value
def refresh():
name = ('Operation Broken Fang Case')
item = sm.get_csgo_item(name, currency='GBP')
print(item)
window.after(300000, refresh)
refresh()
window.mainloop()
I'm trying to get the last 3 labels to refresh the values and display them. Although the values are refreshed, it doesn't display the new values instead of the old ones.
I've looked at a previous question on how to do refreshing, and used said code from the question as a basis
Any help is appreciated,

How to format the text input from Text Widget in Tkinter

In my tkinter program I'm collecting text from the user using Text widget, this is later printed on the screen using a label widget. Although I'm able to print it onto the screen, the text is all center aligned. Since what I'm collecting is a procedure for something it gets difficult to read, so I need it to be left aligned.
This is my Procedure method -
Once the procedure is collected it is stored into a dictionary
def Procedure(self):
textfield = Text(gui, height=30, width=82)
textfield.place(x="20", y="100")
procedure_label = LabelWidget(self.screen, "Procedure", "Courier", 40)
procedure_label.Call().place(x="220", y="20")
button_save = Button(gui, text="Next", padx="50", pady="20", bg="lightgrey",
command=partial(self.CheckPage, 4, procedure=textfield))
button_save.place(x="250", y="600")
This is how I'm printing my proceudre
proc_text_label = ""
for i in fullDictProc:
proc_text_label_temp = Label(root, text=i, wraplength=900)
proc_text_label = proc_text_label_temp
proc_text_label.config(font=("Courier", 12))
proc_text_label.place(x=70, y=250)
Here is a minimal reproducible code to demonstrate the problem
Run it and see the alignment of the text.
from tkinter import *
from functools import partial
gui = Tk()
gui.geometry("700x700")
def printit(textfield):
procedure_list = [textfield.get("1.0", "end-1c")]
textfield.place_forget()
proc_text_label = ""
for i in procedure_list:
proc_text_label_temp = Label(gui, text=i, wraplength=900)
proc_text_label = proc_text_label_temp
proc_text_label.config(font=("Courier", 12))
proc_text_label.place(x=70, y=250)
textfield = Text(gui, height=30, width=82)
textfield.place(x="20", y="100")
button_save = Button(gui, text="Next", padx="50", pady="20", bg="lightgrey",
command=partial(printit, textfield))
button_save.place(x=500, y=600)
gui.mainloop()
I think what you are looking for might be justify:
proc_text_label.config(justify='left')
Have a look at The Tkinter Label Widget
I think what you're looking for is the anchor parameter.
This is how it worked with your minimal example:
from tkinter import *
from functools import partial
gui = Tk()
gui.geometry("700x700")
def printit(textfield):
procedure_list = [textfield.get("1.0", "end-1c")]
textfield.place_forget()
proc_text_label = ""
for i in procedure_list:
proc_text_label_temp = Label(gui, text=i, wraplength=900,
anchor='w',
bg='blue',
width=50)
proc_text_label = proc_text_label_temp
proc_text_label.config(font=("Courier", 12))
proc_text_label.place(x=70, y=250)
textfield = Text(gui, height=30, width=82)
textfield.place(x="20", y="100")
button_save = Button(gui, text="Next", padx="50", pady="20", bg="lightgrey",
command=partial(printit, textfield))
button_save.place(x=500, y=600)
gui.mainloop()

Tkinter label widget showing variable two times

I am trying to create a corona tracker using a entry widget except everytime i try to search two times the search result shows two times. Here is the picture.
I have tried deleting the variable when the button widget but it didnt work.
import sqlite3
import tkinter
db = sqlite3.connect ('covidjakartadb.db')
window = tkinter.Tk()
window.geometry("500x300")
window.title("Corona tracker")
label = tkinter.Label(window, text="Please enter a area")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
def Search_Completed():
# select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)
select_all = "SELECT positive FROM locations WHERE City LIKE '%{0}%'".format( entry.get() )
cursor = sqlite3.Cursor(db)
cursor.execute(select_all)
positive = cursor.fetchall()
print (positive)
tkinter.Label (window, text=positive, font='Ariel 25 bold').pack()
tkinter.Label (window, text="Tips to fight off the coronavirus")
Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()
window.mainloop()
Try this :
import sqlite3
import tkinter
db = sqlite3.connect ('covidjakartadb.db')
window = tkinter.Tk()
window.geometry("500x300")
window.title("Corona tracker")
label = tkinter.Label(window, text="Please enter a area")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
def Search_Completed():
result["text"] = ""
# select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)
select_all = "SELECT positive FROM locations WHERE City LIKE '%{0}%'".format( entry.get() )
cursor = sqlite3.Cursor(db)
cursor.execute(select_all)
positive = cursor.fetchall()
print (positive)
result["text"] = positive
Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()
result= tkinter.Label (window, text=positive, font='Ariel 25 bold')
result.pack()
window.mainloop()

Tkinter entry output

I am creating a Data finder using a db file and i use tkinter entry. however in the console it returns the output [] and the code is this
import sqlite3
import tkinter
db = sqlite3.connect ('covidjakartadb.db')
window = tkinter.Tk()
window.geometry("500x300")
label = tkinter.Label(window, text="Please enter a area")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)
cursor = sqlite3.Cursor(db)
cursor.execute(select_all)
def Search_Completed():
label = tkinter.Label(window, text="aaaaa")
Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()
positive = cursor.fetchall()
print (positive)
window.mainloop()
This project is due today so a answer today would be greatful
The code to select from the database needs to be in the search_completed function. As written it runs before the GUI is even open.
The search should be using the contents of the Entry, entry.get() not entry.
import sqlite3
import tkinter
db = sqlite3.connect ('covidjakartadb.db')
window = tkinter.Tk()
window.geometry("500x300")
label = tkinter.Label(window, text="Please enter a area")
label.pack()
entry = tkinter.Entry(window)
entry.pack()
def Search_Completed():
# select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)
# select_all = "SELECT * FROM locations WHERE '%{0}%'".format( entry.get() )
# This will work
select_all = "SELECT * FROM locations WHERE City LIKE '%{0}%'".format( entry.get() )
# Or this.
# select_all = "SELECT * FROM locations WHERE City == '{0}'".format( entry.get() )
cursor = sqlite3.Cursor(db)
cursor.execute(select_all)
positive = cursor.fetchall()
print (positive)
label = tkinter.Label(window, text="aaaaa")
Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()
window.mainloop()
I'm unable to test the code as I don't have the database so this may have a few errors in it.

get entered text and exit if the text in mass (tkinter)

good day! here's my code:
import tkinter as tk
namemass =["dev", "Dev1"]
self.entry_name = ttk.Entry(self)
self.entry_name.place(x=200, y=50)
btn_cancel = ttk.Button(self, text="cancel", command=self.destroy)
btn_cancel.place(x=300, y=800)
btn_ok = ttk.Button(self, text="ok")
btn_ok.place(x=320, y=170)
so, i have 2 buttons and enter box. I want the program to get the text from the enter box and if namemass list have that inside, then exit. in console program i would code it like that:
name = input()
namemass = ["dev", "Dev1"]
if name in namemass:
import sys
sys.exit()
else:
..........
how to do it using tkinter? thank you in advance!
To fetch the current entry text, use the get method:
current_text = Entry.get()
in your example you can just:
from tkinter import *
import sys
def destroy():
name = entry_name.get()
if name in namemass:
sys.exit()
root = Tk()
namemass = ["dev", "Dev1"]
entry_name = Entry(root)
entry_name.pack()
btn_cancel = Button(root, text="cancel", command=destroy)
btn_cancel.pack()
btn_ok = Button(root, text="ok")
btn_ok.pack()
root.mainloop()
Much easier for Python 3.8, using Walrus. Just add the function for _ok. And add command in btn_ok.
from tkinter import *
import sys
namemass = ["dev", "Dev1"]
def destroy():
#sys.exit()
root.destroy()
def _ok():
if(name_in_list := entry_name.get()) in namemass:
sys.exit()
root = Tk()
entry_name = Entry(root)
entry_name.pack()
btn_cancel = Button(root, text="cancel", command=destroy)
btn_cancel.pack()
btn_ok = Button(root, text="ok", command=_ok)
btn_ok.pack()
root.mainloop()

Categories