Tkinter Error: "_tkinter.TclError: invalid command name ".!frame.!entry" PYTHON - python

I get this error when I insert a value in the textbox.
I want to insert the date in the textbox when selecting it.
Error: _tkinter.TclError: invalid command name ".!frame.!entry"
This is my code:
main.py
from calenderUI import *
from tkinter import *
def Dateselected():
dateSelected=tkCalender()
return str(dateSelected)
def setData():
dateSel=tkCalender()
txtBoxDate.delete(0,END)
txtBoxDate.insert(0,dateSel)
return
root=Tk()
#Frame
miFrame=Frame(root,width=1200,height=2000)
miFrame.pack()
miFrame.config(bg="black")
miLabel=Label(miFrame, text="Date: ",fg="green", bg="black", font="Comic")
miLabel.grid(row=1,column=1,padx=10,pady=10)
txtBoxDate=Entry(miFrame)
txtBoxDate.grid(row=1,column=2,padx=10,pady=10,ipadx=40,ipady=2.5)
buttonDate=Button(miFrame, text="Date",command=lambda:setData())
buttonDate.grid(row=1,column=3,padx=10,pady=10)
root.mainloop()
calenderUI.py
from tkcalendar import Calendar
from tkinter import Tk
from tkinter import ttk
def tkCalender():
def print_sel():
print(cal.selection_get())
root = Tk()
cal=Calendar(root)
cal.pack()
ttk.Button(root, text="Select",command=print_sel).pack()
root.mainloop()
if __name__ == '__tkCalender__':
tkCalender()
Waiting for your help, thanks!

Error fixed, just remove root.mainloop() and some changes to insert the date in textbox (calenderUI.py)
main.py
from calenderUI import *
from tkinter import *
def Dateselected():
dateSelected=tkCalender()
return str(dateSelected)
def setData():
dateSel=tkCalender(txtBoxDate)
root=Tk()
#Frame
miFrame=Frame(root,width=1200,height=2000)
miFrame.pack()
miFrame.config(bg="black")
miLabel=Label(miFrame, text="Date: ",fg="green", bg="black", font="Comic")
miLabel.grid(row=1,column=1,padx=10,pady=10)
txtBoxDate=Entry(miFrame)
txtBoxDate.grid(row=1,column=2,padx=10,pady=10,ipadx=40,ipady=2.5)
buttonDate=Button(miFrame, text="Date",command=setData)
buttonDate.grid(row=1,column=3,padx=10,pady=10)
root.mainloop()
calenderUI.py
from tkcalendar import Calendar
from tkinter import *
dateSelected=""
def tkCalender(e):
def print_sel():
dateSelected=str(cal.selection_get())
txtBoxDate=e.delete(0,END)
txtBoxDate=e.insert(0,dateSelected)
return dateSelected
root = Tk()
cal=Calendar(root)
cal.pack()
ttk.Button(root, text="Select",command=print_sel).pack()
return str(print_sel())
if __name__ == '__tkCalender__':
tkCalender()

Related

How can i show the progressbar tkinter while executing another function

import camelot
import ghostscript
import os
from tkinter import *
from tkinter import Tk,Canvas,filedialog
from tkinter.ttk import Progressbar
def dataExtraction(file):
fileName=os.path.basename(file)
fileName=filename.replace('.pdf',"")
tables=camelot.read_pdf(file,pages='all')
finalData=pd.DataFrame()
for i,j in enumerate(tables):
data=tabs[i].df
finalData.append(data)
finalData.to_excel(filename+'.xlsx')
def tkWindow():
root=Tk()
w=root.winfo_screenwidth()
h=root.winfo_screenheight()
root.geometry("%d%d" % (w,h))
my_canvas=Canvas(root,width=w,height=h)
my_canvas.pack(fill=both,expand=True)
####upload button####
def openPdf():
global pdffile
pdffile=filedialog.askopenfilename()
return pdffile
upload=Button(root,
width=20,
pady=5,
text='Upload Pdf',
command=openPdf)
pdf_win=my_canvas.create_window(10,350,anchor='nw',window=upload)
###submit button###
def run():
dataExtraction(file)
runbtn=Button(root,
width=10,
pady=5,
command=threading.Thread(target=run).start)
run_win=my_canvas.create_window(550,590,anchor='nw',window=runbtn)
root.mainloop()
tkWindow()
I want to add a progressbar when i am calling dataExtraction function inside run and it will show the progress till the dataExtraction executed once done progressbar should automatically disappear. If anyone can suggest me how can i do that that will be great.

Customtkinter , root.focus_get() dont detect if the focus is on custom tkinter entrys

when I run this code for **Exemple ** then I click on the ttk entry and press enter it print ----> "ttk_entry"
But when I do the same thing to the customtkinter.CTkEntry it print **nothing **.
import customtkinter
from tkinter import *
def test(event):
if root.focus_get() == custom_entry:
print("custom_entry")
elif root.focus_get() == ttk_entry:
print("ttk_entry")
root = customtkinter.CTk()
custom_entry = customtkinter.CTkEntry(root) #CTkEntry
custom_entry.pack()
ttk_entry = Entry(root) #ttk_entry
ttk_entry.pack()
root.bind("<Return>", test)
root.mainloop()
I was expecting that it print "custom_entry" .

second window run first how to stop it

when i run python file kill_app its run first 2nd python file(Hb_test.py) then run 1st python on tkinter.i am using vs code and python version is 3.10
see the code below
from tkinter import*
from Lab.Hb_Test import Hba
class kill_App:
def __init__(self,root):
self.root = root
self.root.geometry("1350x700+0+0")
self.root.title("Billing Software")
bg_color = "#074463"
title = Label(self.root,text = "PATHOLAB",bd=12,relief=GROOVE,bg=bg_color,fg="white",
font = ("ALGERIAN",40),pady=2).pack(fill=X)
if __name__=="__main__":
root =Tk()
obj = kill_App(root)
root.mainloop()
****
this is my second
**
**from tkinter import*
from tkinter import ttk
class Hba:
def __init__(self,root):
self.root = root
self.root.geometry("300x70+200+200")
self.root.title("JIBAN PRABHA PATHOLAB")
bg_color = "#074463"
Hb = LabelFrame(self.root,text="BLOOD TEST",font=("ALGERIAN",15,"bold")
,fg="gold",bg=bg_color)
Hb.place(x=0,y=0)
self.hb_neu_lbl = Label(Hb,text="Hb%(sahils) Test",bg=bg_color,fg="white",font=("Bell MT",15,"bold")).grid(row=1,column=1,padx=5,pady=5)
self.hb_neu=Entry(Hb,width=10,font="BellMT 15",bd=5,relief=SUNKEN).grid(row=1,column=2,padx=5,pady=5)
root = Tk()
obj = Hba(root)
root.mainloop()**
**
When Hb_Test is imported, its code will be executed, so the last three lines in the module will create a window:
Hb_Test.py
...
# below lines will create a window
root = Tk()
obj = Hba(root)
root.mainloop()
Note that it is not recommended to create more than one instance of Tk() and execute .mainloop() more than once.
For window other than the main/root window, use Toplevel instead of Tk. For your case, I would suggest that Hba inherits from Toplevel:
import tkinter as tk
class Hba(tk.Toplevel):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.geometry("300x70+200+200")
self.title("JIBAN PRABHA PATHOLAB")
bg_color = "#074463"
Hb = tk.LabelFrame(self, text="BLOOD TEST", font=("ALGERIAN",15,"bold"), fg="gold", bg=bg_color)
Hb.place(x=0, y=0)
self.hb_neu_lbl = tk.Label(Hb, text="Hb%(sahils) Test", bg=bg_color, fg="white", font=("Bell MT",15,"bold"))
self.hb_neu_lbl.grid(row=1,column=1,padx=5,pady=5)
self.hb_neu = tk.Entry(Hb, width=10, font="BellMT 15", bd=5, relief=tk.SUNKEN)
self.hb_neu.grid(row=1,column=2,padx=5,pady=5)
Note that I have changed from tkinter import * to import tkinter as tk because wildcard import is not recommended as well.
Also don't write code like below:
self.hb_neu_lbl = Label(Hb,text="Hb%(sahils) Test",bg=bg_color,fg="white",font=("Bell MT",15,"bold")).grid(row=1,column=1,padx=5,pady=5)
because self.hb_neu_lbl will be None (result of .grid(...)). Code like below instead:
self.hb_neu_lbl = Label(Hb, text="Hb%(sahils) Test", bg=bg_color, fg="white", font=("Bell MT",15,"bold"))
self.hb_neu_lbl.grid(row=1,column=1,padx=5,pady=5)

simple window with an enter field

i'm a new programmer and there are certainly several errors but this shouldn't be difficult to spot. I need to create a simple window with a field named "Concorrente 1:" and an entry field displayed by function named lacopertina(). I don't understand where is the error:
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
class schermoiniziale(tk.Frame):
def lacopertina():
print(gio1)
#return (tot1)
def __init__(self):
global gio1
#tot1=0
#schermo1=Tk()
self.gio1=tk.StringVar()
lab1=ttk.Label(self, text="Concorrente 1:")
lab1.pack()
ent1=ttk.Entry(self, textvariable=self.gio1)
ent1.pack()
pulsante = ttk.Button(self, text="Inizio", textvariable=self.gio1, command=self.lacopertina)
pulsante.pack()
def main():
schermoiniziale().mainloop()
if __name__== "__main__":
main()
I would suggest you to go through some tutorials on Python OOP.
I have modified your code as below with some comment:
# avoid using wildcard import
import tkinter as tk
from tkinter import ttk
class schermoiniziale(tk.Frame):
def __init__(self, master, **kw):
# need to call __init__() of inherited class
super().__init__(master, **kw)
self.gio1 = tk.StringVar()
lab1 = ttk.Label(self, text="Concorrente 1:")
lab1.pack()
ent1 = ttk.Entry(self, textvariable=self.gio1)
ent1.pack()
# removed textvariable=self.gio1 as I think you actually don't need it
pulsante = ttk.Button(self, text="Inizio", command=self.lacopertina)
pulsante.pack()
def lacopertina(self):
# use .get() to get the content of a StringVar
print(self.gio1.get())
def main():
# need to create the root window before creating other widget
root = tk.Tk()
# pass root window as the parent of the widget
frame = schermoiniziale(root)
frame.pack()
# start the tkinter mainloop
root.mainloop()
if __name__== "__main__":
main()

python starter screenshot using thread

after going through some stack overflow and external guides still was stuck with an odd problem thread starts but function not executing but used same type of setup in unity c# and visual studio C# windows application but works there fine any help would be appreciated
import tkinter
import threading
import PIL
import time
from datetime import datetime
from PIL import Image
import pyscreenshot as ImageGrab
from tkinter import *
from tkinter import messagebox
from tkinter import Tk, Label, Button, Entry
running=False
class app:
def __init__(self, master):
self.master = master
self.thread1=threading.Thread(target=self.screenshot)
master.title("Screenshot Taker")
Label(master, text="Interval between screenshots").grid(row=0,sticky=W)
self.E1=Spinbox(master, from_ = 5, to = 1000,state = "readonly")
self.E1.grid(row=0,column=2,padx=10)
Label(master, text="File prefix").grid(row=1,sticky=W)
self.E2=Entry(master)
self.E2.grid(row=1,column=2,padx=10)
self.B1=Button(master, text="start" ,command = self.start)
self.B1.grid(row=2,column=0,columnspan=3)
self.B2=Button(master, text="stop" ,command = self.stop)
self.B2.grid(row=2,column=1,columnspan=3)
def screenshot(self):
while(running):
im=ImageGrab.grab()
dt_string = datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
name=str(self.E2.get())+str(dt_string)
logging.debug(name)
im.save(name+".png")
time.sleep(int(self.E1.get()))
def start(self):
running=True
self.thread1.start()
def stop(self):
running=False
self.thread1.join()
root = Tk()
app = app(root)
root.resizable(False, False)
root.mainloop()```
To access a global variable in python, you need to use "global var_name" in each function that needs to access it:
import tkinter
import threading
import PIL
import time
from datetime import datetime
from PIL import Image
import pyscreenshot as ImageGrab
from tkinter import *
from tkinter import messagebox
from tkinter import Tk, Label, Button, Entry
running=False
class app:
def __init__(self, master):
self.master = master
self.thread1=threading.Thread(target=self.screenshot)
master.title("Screenshot Taker")
Label(master, text="Interval between screenshots").grid(row=0,sticky=W)
self.E1=Spinbox(master, from_ = 5, to = 1000,state = "readonly")
self.E1.grid(row=0,column=2,padx=10)
Label(master, text="File prefix").grid(row=1,sticky=W)
self.E2=Entry(master)
self.E2.grid(row=1,column=2,padx=10)
self.B1=Button(master, text="start" ,command = self.start)
self.B1.grid(row=2,column=0,columnspan=3)
self.B2=Button(master, text="stop" ,command = self.stop)
self.B2.grid(row=2,column=1,columnspan=3)
def screenshot(self):
global running
while(running):
im=ImageGrab.grab()
dt_string = datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
name=str(self.E2.get())+str(dt_string)
logging.debug(name)
im.save(name+".png")
time.sleep(int(self.E1.get()))
def start(self):
global running
running=True
self.thread1.start()
def stop(self):
global running
running=False
self.thread1.join()
root = Tk()
app = app(root)
root.resizable(False, False)
root.mainloop()

Categories

Resources