python 3.6 Tkinter Label attribute error when using .grid - python

I am beginning to get the hang of tkinter, but I have run into a problem. I want to create a label that displays an appropriate message according to the values of an Entry field, checked by a button. When this is used repeatedly, using just the tk.Label command will just overlay on top of the existing label, So I am trying to assign the label to a variable:
messagebox=tk.Label(root2,text=" ",font(style,font).grid(row=1,column=0,columnspan = 50))
I want to later on use the .configure command to change this text, However I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:[CENSORED BY POSTER (me), It was just the file directory]", line 84, in Checkpass
messagebox = tk.Label(root2,text=" ",font=(style,font).grid(row=1,column=0,columnspan = 50))
AttributeError: 'tuple' object has no attribute 'grid'
Can anyone please tell me what I am doing wrong and explain why it wont work.
Thank you,
Tava

Check your parentheses at font(style,font).grid. Probably should be:
messagebox=tk.Label(root2,text=" ",font(style,font)).grid(row=1,column=0,columnspan = 50)
But I personally recommend split it into two lines for the sake of readability:
messagebox = tk.Label(root2, text=" ", font(style, font))
messagebox.grid(row=1, column=0, columnspan = 50)

Related

When opining a PDF with Python tkPDFViewer throws an error, is there an mistake I'm not seeing?

When testing the opening of a PDF for a larger project the code throws an error. The error emanates from the tkPDFViewer.
Any ideas?
Here is the error:
Exception in thread Thread-1 (add_img): Traceback (most recent call last):
File "C:\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner self.run()
File "C:\Python\Python310\lib\threading.py", line 946, in run self._target(*self._args, *self._kwargs)
File "C:\Python\Python310\lib\site-packages\tkPDFViewer\tkPDFViewer.py", line 46, in add_img
pix = page.getPixmap()
AttributeError: 'Page' object has no attribute 'getPixmap'. Did you mean: 'get_pixmap'?
import tkinter as tk
from tkPDFViewer import tkPDFViewer as pdf
print('Starting TestPDF2')
mainWindow = tk.Tk()
# Set the width and height of our root window.
mainWindow.geometry("550x750")
# creating object of ShowPdf from tkPDFViewer.
v1 = pdf.ShowPdf()
# Adding pdf location and width and height.
v2 = v1.pdf_view(mainWindow, pdf_location = 'testpdf.pdf', width = 50, height = 100)
# Placing Pdf in my gui.
v2.pack()
mainWindow.mainloop()
print('End TestPDF2')
This is an internal problem with tkPDFViewer. The error is quite self-explanatory - getPixmap was deprecated and removed in favor of get_pixmap in one of the dependencies of tkPDFViewer (see here). So the library needs to get fixed (the methods need to be renamed to match the new version) and it doesn't look very well maintained. You can try opening an issue or a PR on its GitHub. But I'd probably lean towards saying this library is dead and broken, and finding yourself an alternative that works.

getting error when closing the script problem

I don't know what is problem in this code .it runs perfectly
in script .but after closing the program it shows this error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\python3.9\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "C:\Users\NK Infosystem\PycharmProjects\password tkinter\main.py", line 225, in login_in
if uni_username.get() == u_name and uni_pass.get() == u_pass:
File "C:\python3.9\lib\tkinter\__init__.py", line 3040, in get
return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".!entry"
and 225 line is this
cursr.execute("SELECT rowid ,* FROM usernameandpassword")
user_and_pass = (cursr.fetchall())
for users in user_and_pass:
u_name = (users[1])
u_pass = (users[2])
if uni_username.get() == u_name and uni_pass.get() == u_pass:
show_new_user_window()
uni_username.delete(0, "end")
uni_pass.delete(0, "end")
save_uni_pass_and_name = Button(mainscreen, text="Save", command=save_uni_ones, padx=30, bg="blue2", fg="gold").place(
x=113, y=170, anchor="c")
login_butt = Button(mainscreen, text="Login", padx=30, command=login_in, bg="blue2", fg="gold").place(x=213, y=170,
anchor="c")
connection.commit()
mainscreen.mainloop()
full code is at patch1 branch
https://github.com/aadityabaj/python-projects.git
It is actually not a problem: The error says that python tries to get the input of an Entry that doesn't exist (anymore). When you close the window, you destroy also the Entry. So actually I think It's nothing to worry about.
Tip:
when you try to add a mainscreen.quit() command.
I've seen this problem before and I hope it's helping you.
update 1
you could make a button to quit and add it to a function or just add it to some hotkeys or to an if-statement.
if my answer is helpful, please vote it up, because at 50 points of the reputation I can use comments and that's very handy. I've now 40

Was told to use select method of tkinter Checkbutton. It doesn't work. How do I have it chacked if the stored setting is true?

I'm trying to make a GUI for my app, but I have hit a roadblock when trying to make a settings Toplevel. This Toplevel comes with tabs and setting buttons that start at the same state their respective settings are stored as from last settings/defaults.
Here is the exception I am currently getting:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "KMIU 4\kmiu_dv7.pyw", line 38, in settings
sett.display(main)
File "KMIU 4\bk\settings.py", line 54, in __init__
self.windowsettings(tab2)
File "KMIU 4\bk\settings.py", line 23, in windowsettings
if settings['Fullscreen'].get(): fs_butt.select()
AttributeError: 'Checkbutton' object has no attribute 'select'
Here is the bit of code that is causing the issue:
def windowsettings(self, tab):
global settings
text = Label(tab, text ="sample text")
text.grid(columnspan = 2)
fs_butt = Checkbutton(
tab,
text="Fullscreen",
command=lambda: settings['Fullscreen'].set(not settings['Fullscreen'].get()))
print(settings)
fs_butt.grid(row=1)
if settings['Fullscreen'].get(): fs_butt.select()
For me in my code with tk.Checkbutton, select() works, and ive seen some other people having the same issue, not sure whats causing it(maybe your using ttk.Checkbutton), but here is a way around:
First assign a BooleanVar() to your checkbutton:
var = BooleanVar()
....
fs_butt = Checkbutton(tab,variable=var,......) #same for ttk.Checkbutton(..) too
Now to set the value of the variable to True, to select, and False to deselect:
if settings['Fullscreen'].get():
var.set(True)
Or maybe your using ttk.Checkbutton which does not have select() and deselect()

IndexError out of range when changing styles

So far in a program I have been writing, I have used basic tkinter for widgets. However, after some Googling, I found out about ttk and its themed widgets.
According to the documentation on the Python Docs site, I only needed to add from tkinter.ttk import * to make use of the ttk widgets without needing to change any code, so it seemed worth it to me.
Of course, the only thing that wouldn't work is the -bg flag. However, when I try and use the ttk styling, I get:
IndexError: list index out of range
of all things, though I wrote nothing in code referred to a range.
This is the particular section of code:
style = Style()
# stuff
style.map("rbstyle", background="grey")
# stuff
crBox = Label(win, width=6, textvariable=cr,
state='normal', relief='sunk', style="rbstyle")
and this is the output:
Traceback (most recent call last):
File "C:\Users\James\Google Drive\Queens' Athletics\window.py", line 83, in <module>
style.map("rbstyle", background="grey")
File "C:\Python34\lib\tkinter\ttk.py", line 402, in map
self.tk.call(self._name, "map", style, *(_format_mapdict(kw))))
File "C:\Python34\lib\tkinter\ttk.py", line 111, in _format_mapdict
_format_optvalue(_mapdict_values(value), script)))
File "C:\Python34\lib\tkinter\ttk.py", line 85, in _mapdict_values
state[0] # raise IndexError if empty
IndexError: list index out of range
>>>
Why am I having this error? I followed exactly what it said on Python Docs.

Entry().get() doesn't work in Python 3.4

Despite all attempts, I cannot seem to get Entry().get() to assign a string in an Entry window. Here's a code snippet:
Itx_bcn_ent = Entry(win).grid(row=1,column=1)
I define a button to call a function:
btn = Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)
Here's the function:
def getnums():
Itx_bcn = Itx_bcn_ent.get()
When I run the script, I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Python34\voltage_substate_GUI.py", line 7, in getnums
Itx_bcn = Itx_bcn_ent.get()
AttributeError: 'NoneType' object has no attribute 'get'
I've seen the construct to use a Class StringVar() and the option "textvariable=" with the Entry() object, however doing this seems overly complicated as it just creates an additional set of variables between what's in the Entry window and the variable I am trying to assign.
Any thoughts on this?
Entry.grid() returns None, so when you do something = Entry(root).grid(), you're getting something=None.
This isn't a problem until you try to use that thing! That's why you're getting a 'NoneType' object has no attribute 'get' error.
Itx_bcn_ent = Entry(win)
Itx_bcn_ent.grid(row=1,column=1)
Now your button works :), though you have the same problem with your btn = line. I've yet to have to reuse that assignment, so maybe just drop it?
Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)

Categories

Resources