Why does my Tkinter window crash when I try to display a label? - python-3.x

I am using Pycharm and Python 3.8.
My Tkinter window stops responding and causes my program to crash.
Pycharm presents the following line in its output: Process finished with exit code -805306369 (0xCFFFFFFF)
I have the following code for displaying the progress of a for loop:
import tkinter as tk
from tkinter import filedialog, ttk
from tkinter.ttk import Progressbar
import json
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.filename = ''
tk.Button(self, text = 'Upload File', command = self.get_file).grid(row = 0, column = 0)
tk.Button(self, text = 'Verify', command = self.verify).grid(row = 1, column = 0)
# tk.Label(self, text = 'some name').grid(row = 0, column = 1)
self.mainloop()
def get_file(self):
self.filename = tk.filedialog.askopenfilename(title = "Select File", filetypes = [("JSON", "*.json")])
tk.Label(self, text = self.filename).grid(row = 0, column = 1)
def verify():
lbl = tk.Label(self, text = 'Verifying Trustee Public Keys')
lbl.grid(row = 2, column = 0)
prog1 = ttk.Progressbar(self,
length = 100,
orient = "horizontal",
maximum = len(self.data["trustee_public_keys"]),
value = 0)
prog1.grid(row = 3, column = 0)
i = 1
for trustee_key_list in self.data['trustee_public_keys']:
print('Trustee :', i)
//some code
i = i+1
prog1['value'] = i
The get_file part works. But, when I click the verify button to execute the verify() function. The entire window freezes and I get a not responding message.
WHat is the problem with the following part of my code?
lbl = tk.Label(self, text = 'Verifying Trustee Public Keys')
lbl.grid(row = 2, column = 0)
What am I doing wrong?

Related

Separate fuction for each button

Trying to create a python program in tkinter to mark attendance of persons engaged in a special duty. By clicking the button containing their name the figure right to their name is incremented by 1. While i created the function when any button is placed all figures are got incremented. And the attempt finally reached here.
from tkinter import *
staff=['GEORGE ', 'JAMES ', 'THOMAS', 'MATHEW',
'CLETUSCRUZ', 'FREDY', 'PAUL', 'MEGANI', 'BILL',
'JULIA ']
def factory(number):
def f():
number.set(number.get()+1)
return f
functions =[]
for i in range(10):
functions.append(factory(i))
for i in functions:
f()
window = Tk()
window.title(" Duty List")
window.geometry('320x900')
number = IntVar()
row_value =3
for i in staff:
ibutton = Button(window, text= i, command=clicked)
ibutton.grid(column=1, row=row_value)
ilabel = Label(window, textvariable=number)
ilabel.grid(column=2,row=row_value)
row_value+=1
window.mainloop()`
Factory now creates a unique IntVar for each individual.
link connects button press to onclick for processing IntVar
I've used columnconfigure to push the numbers to the right hand side of window
import tkinter as tk
staff=["GEORGE ", "JAMES ", "THOMAS", "MATHEW",
"CLETUSCRUZ", "FREDY", "PAUL", "MEGANI", "BILL",
"JULIA "]
window = tk.Tk()
window.title(" Duty List")
window.columnconfigure(1, weight = 1)
def factory(n):
return tk.IntVar(value = n)
def onclick(a):
b = a.get()+1
a.set(b)
def link(a, b):
return lambda: a(b)
for i,n in enumerate(staff):
b = factory(0)
a = tk.Button(window, text= n, bd = 1)
a.grid(column = 0, row = i, sticky = tk.NW, padx = 4, pady = 4)
a["command"] = link(onclick, b)
l = tk.Label(window, text = "", textvariable = b, anchor = tk.NW)
l.grid(column = 1, row = i, sticky = tk.NE, padx = 4, pady = 4)
window.geometry("200x319")
window.resizable(False, False)
window.mainloop()

Python tkinter - label not showing on the 2nd screen

I created a code with a yes/no question, and if yes, I use an entry box to ask how many. But when I reach to that How many question, the label is not showing and I don't understand why?
Thanks in advance, below is the code:
from tkinter import filedialog, messagebox, ttk, constants
from tkinter import *
root = Tk()
root.focus_force()
root.withdraw()
root.call('wm', 'attributes', '.', '-topmost', True)
yesnob = messagebox.askyesno('Test','Do you have a clue?')
if yesnob == True:
root2 = Tk()
root2.call('wm', 'attributes', '.', '-topmost', True)
root2.wm_title('How many ?')
nb_b = 0
title_loop = Label(root2, textvariable = 'How many ?', height = 2, width = 15)
title_loop.grid(row = 1, column = 0)
entrybox = Entry(root2, textvariable = nb_b, width = 5)
entrybox.grid(row = 2, column = 0)
def get_data():
global nb_b
try:
nb_b = int((entrybox.get()))
except ValueError:
no_int = messagebox.showerror('Error', 'You did not enter a number, try again!')
root.destroy()
root2.destroy()
exit_but = Button(root2, text = 'OK', command = get_data, height = 3, width = 5)
exit_but.grid(row = 3, column = 1)
root2.mainloop()
else:
root.destroy()
root.mainloop()
Changing the "textvariable" to "text" worked for me:
title_loop = Label(root2, text = 'How many ?', height = 2, width = 15)
You created the Label with the textvariable argument. If you change it to text the label is shown:
title_loop = Label(root2, text= 'How many ?', height = 2, width = 15)
textvariable can be used in combination with a StringVar if you want to have a text that can be changed. If the text is static use the text argument.

resizing image in tkinter for my browsing file code

how can I resize my image? here is my code..
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import Image, ImageTk
class Root(Tk):
def __init__(self):
super(Root, self).__init__()
self.title("Python Tkinter Dialog Widget")
self.minsize(640, 400)
self.labelFrame = ttk.LabelFrame(self, text = "Open File")
self.labelFrame.grid(column = 0, row = 1, padx = 20, pady = 20)
self.button()
def button(self):
self.button = ttk.Button(self.labelFrame, text = "Browse A File",command = self.fileDialog)
self.button.grid(column = 1, row = 1)
def fileDialog(self):
self.filename = filedialog.askopenfilename(initialdir = "/", title = "Select A File", filetype =
(("jpeg files","*.jpg"),("all files","*.*")) )
self.label = ttk.Label(self.labelFrame, text = "")
self.label.grid(column = 1, row = 2)
self.label.configure(text = self.filename)
img = Image.open(self.filename)
photo = ImageTk.PhotoImage(img)
self.label2 = Label(image=photo)
self.label2.image = photo
self.label2.grid(column=3, row=4)
root = Root()
root.mainloop()
You can either use Image.resize() which does not keep the image aspect ratio, or Image.thumbnail() which keeps the image aspect ratio:
img = Image.open(self.filename)
imgsize = (600, 400) # change to whatever size you want
#img = img.resize(imgsize)
img.thumbnail(imgsize)
photo = ImageTk.PhotoImage(img)

How to make new label when the program is running in tkinter?

I want to make a new label when the program is running when the button is pressed. I am working in Tkinter python v3.
Here is my code:
from tkinter import *
from tkinter import ttk
import tkinter as tk
import copy
class App(ttk.Frame):
def __init__(self, master):
self.newwindow = master
self.pocetnik_label = Label(master, text = 'Pocetnik')
self.pocetnik_label.pack(side = LEFT)
self.dodaj_button = Button(master, text = '+', command = self.pocetnik)
self.dodaj_button.pack(side = RIGHT)
self.newwindow.mainloop()
def pocetnik(self):
b2= tk.Toplevel(self.newwindow)
self.ime_label = Label(b2, text = 'Ime').grid(row = 0, column = 0)
self.ime_entry = Entry(b2, bd = 5).grid(row = 0, column = 1)
self.vreme_label = Label(b2, text = 'Vreme').grid(row = 1, column = 0)
self.vreme_entry = Entry(b2, bd = 5).grid(row = 1, column = 1)
self.napravi_button = Button(b2, text = 'Napravi').grid(row = 3, column = 0)
master = Tk()
pocetnik = App(master)
I've checked your code on my computer and it kind of works :-)
If I press the '+' button a new window appears.
from tkinter import ttk
import tkinter as tk
import copy
class App(ttk.Frame):
def __init__(self, master):
self.newwindow = master
self.pocetnik_label = Label(master, text='Pocetnik')
self.pocetnik_label.pack(side=LEFT)
self.dodaj_button = Button(master, text='+', command=self.pocetnik)
self.dodaj_button.pack(side=RIGHT)
self.newwindow.mainloop()
def pocetnik(self):
b2 = tk.Toplevel(self.newwindow)
self.ime_label = Label(b2, text='Ime').grid(row=0, column=0)
self.ime_entry = Entry(b2, bd=5).grid(row=0, column=1)
self.vreme_label = Label(b2, text='Vreme').grid(row=1, column=0)
self.vreme_entry = Entry(b2, bd=5).grid(row=1, column=1)
self.napravi_button = Button(b2, text='Napravi').grid(row=3, column=0)
master = Tk()
pocetnik = App(master)
Maybe you want to become this new window a modal dialoge? You could have a look at How to create a modal dialog in tkinter?

Error: Attribute error in TCL

I am trying to create an application in Python GUI using tkinter. Here is the code I'm using for the GUI part. Whenever I try to access my Entry Widget I get error.
Ex:Sin_put.get() should give me the text in the Entry widget but it gives me an error
AttributeError: 'NoneType' object has no attribute 'get'
I'm relatively new to Python. So if you guys have any suggestions to improve the functionality you're most welcome.
from tkinter import *
from tkinter import ttk
sys.path.insert(0, 'home/ashwin/')
import portscanner
class App:
def __init__(self, master):
master.option_add('*tearOff', False)
master.resizable(False,False)
self.nb = ttk.Notebook(master)
self.nb.pack()
self.nb.config(width=720,height=480)
self.zipframe = ttk.Frame(self.nb)
self.scanframe = ttk.Frame(self.nb)
self.botframe = ttk.Frame(self.nb)
self.mb = Menu(master)
master.config(menu = self.mb)
file = Menu(self.mb)
info = Menu(self.mb)
self.mb.add_cascade(menu = file, label = 'Tools')
self.mb.add_cascade(menu = info, label = 'Help')
file.add_command(label='Zip Cracker', command = self.zipframe_create)
file.add_command(label='Port Scanner', command = self.scanframe_create)
file.add_command(label='Bot net', command =self.botframe_create)
info.add_command(label='Usage', command=(lambda:print('Usage')))
info.add_command(label='About', command=(lambda:print('About')))
def zipframe_create(self):
self.nb.add(self.zipframe,text='Zip')
self.zipframe.config(height=480,width=720)
zlabel1 = ttk.Label(self.zipframe, text='Select the zip file').grid(row=0,column=0, padx=5, pady=10)
zlabel2 = ttk.Label(self.zipframe, text='Select the dictionary file').grid(row=2,column=0, padx=5)
ztext1 = ttk.Entry(self.zipframe, width = 50).grid(row=0,column=1,padx=5,pady=10)
ztext2 = ttk.Entry(self.zipframe, width = 50).grid(row=2,column=1,padx=5)
zoutput = Text(self.zipframe, width=80, height=20).grid(row=3,column=0,columnspan = 3,padx=5,pady=10)
zb1 = ttk.Button(self.zipframe, text='Crack', width=10).grid(row=0,column=2,padx=5,pady=10)
def scanframe_create(self):
self.nb.add(self.scanframe,text='Scan')
self.scanframe.config(height=480,width=720)
slabel1 = ttk.Label(self.scanframe, text='IP address').grid(row=0,column=0, padx=5, pady=10)
sin_put = ttk.Entry(self.scanframe, width = 50).grid(row=0,column=1,padx=5,pady=10)
soutput = Text(self.scanframe, width=80, height=20).grid(row=3,column=0,columnspan = 3,padx=5,pady=10)
sb1 = ttk.Button(self.scanframe, text='Scan', width=6,command= print('Content: {}'.format(sin_put.get()))).grid(row=0,column=2,padx=5,pady=10)
def botframe_create(self):
self.nb.add(self.botframe,text='Bot')
self.botframe.config(height=480,width=720)
blabel1 = ttk.Label(self.botframe, text='IP address').grid(row=0,column=0, padx=5, pady=10)
blabel2 = ttk.Label(self.botframe, text='Username').grid(row=1,column=0, padx=2)
blabel3 = ttk.Label(self.botframe, text='password').grid(row=2,column=0, padx=2)
btext1 = ttk.Entry(self.botframe, width = 30).grid(row=0,column=1,padx=5,pady=10)
btext2 = ttk.Entry(self.botframe, width = 30).grid(row=1,column=1)
btext2 = ttk.Entry(self.botframe, width = 30).grid(row=2,column=1)
boutput = Text(self.botframe, width=80, height=20).grid(row=3,column=0,columnspan = 3,padx=5,pady=10)
bb1 = ttk.Button(self.botframe, text='Connect', width=8).grid(row=2,column=2,padx=5,pady=10)
def main():
root = Tk()
feedback = App(root)
root.mainloop()
if __name__ == "__main__": main()
grid() returns None so sin_put will always equal None. Instead of passing the Tkinter ID to grid() you have to store it first if you want to reference it later. Note that for the Buttons, putting it all on one line is fine as you don't use the button's ID later.
sin_put=ttk.Entry(self.scanframe, width = 50) ## stores the return ID from Entry
sin_put.grid(row=0,column=1,padx=5,pady=10) ## don't catch return from grid as it is None

Resources