I have searched quite a bit,but I couldn't find a solution to this. I'm trying to create a registration form using tkinter which later on i shall connect to a database. Here is the code :
from Tkinter import *
class MWindow(object):
def __init__(self,master):
self.frame=Frame(master)
self.frame.pack()
self.title= Label(self,text = "Login")
self.title.grid(row=0,column=1)
self.userid_label = Label(self,text ="Username: ")
self.userid_label.grid(row=1,column=0)
self.userid_entry= Entry(self)
self.userid_entry.grid(row=1,column=1)
self.password_label = Label(self,text ="Password: ")
self.password_label.grid(row=2,column=0)
self.password_entry= Entry(self)
self.password_entry.grid(row=2,column=1)
self.signin = Button (self,text = "Login",command=logging_in)
self.signin.grid(row=5,column=1)
self.signup = Button (self,text = "Sign Up",command=signing_up)
self.signin.grid(row=5,column=2)
def logging_in(self):
pass
def signing_up(self):
pass
root= Tk()
root.attributes('-fullscreen',True)
root.resizable(width=False, height=False)
root.title("My Registration Form")
app=MWindow(root)
root.mainloop()
Here is the error i get :
Traceback (most recent call last):
File "form.py", line 41, in
app=MWindow(root)
File "form.py", line 11, in init
self.title= Label(self,text = "Login")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2591, in init
Widget.init(self, master, 'label', cnf, kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2081, in init
BaseWidget._setup(self, master, cnf)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2059, in _setup
self.tk = master.tk
AttributeError: 'MWindow' object has no attribute 'tk'
I tried going to the library files to understand what's wrong,but being a beginner i can't make much of it. Some explanation of what's going wrong and why would be very helpful.
You're passing self in as the master / parent to your widgets.
e.g - Entry(self, ...) But, your class MWindow doesn't inherit from a Tkinter widget.
Perhaps you meant to use self.frame?
If you really want to use self you could do this:
import Tkinter as tk
...
class MWindow(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
abutton = tk.Button(self, ....)
If this is confusing, then here's a pretty good answer.
Since you mentioned the source code....
Look at the Tk() class. Which contains the following line:
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
Now, check out the BaseWidget class which all Widget's inherit from. This contains the following line:
self.tk = master.tk
You have you're base root window Tk() which has the attribute tk and every child of this set's an attribute tk to be the master's tk attribute. So on and so forth for nested widgets, since the parent of a widget could just be another widget it doesn't have to be the root window of course.
Related
i'm working with classes on tkinter and i have this problem:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 549, in _clicked
self._command()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\input_frame.py", line 88, in go_back
from main import SerialFrame
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 126, in <module>
SerialFrame(root).place(x=25, y=50)
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 20, in __init__
self.createWidgetsMain()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 101, in createWidgetsMain
refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 106, in __init__
self._draw()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 261, in _draw
self._update_image() # set image
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 172, in _update_image
self._image_label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1675, in configure
return self._configure('configure', cnf, kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1665, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
This is the code on my application and explanation about how it should work:
First of all, i have a file with the class SerialFrame, and the creation of the window and the frame:
class SerialFrame(customtkinter.CTkFrame):
# CONSTRUCTOR FOR THE FRAME
def __init__(self, master, *args, **kwargs):
super(SerialFrame, self).__init__(master)
self.master = master
self.serial_port = ""
self.configure(width=400, height=400)
self.createWidgetsMain()
# METHOD TO CREATE ALL WIDGETS
def createWidgetsMain(self):
...
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
And i have another 2 files with other diferent classes for other frames in similar way.
The problem is when i press a button to go back to the first frame, here is the code in the other classes:
def go_back():
self.destroy()
btn_back.destroy()
from main import SerialFrame
SerialFrame(self.master).place(x=25, y=50)
btn_back = customtkinter.CTkButton(self.master, text="Go Back",
command=go_back, cursor="hand2")
btn_back.place(x=465, y=400)
Obviously, while coding the app i had many different problems and if you see something that shouldn't be work well, you can tell me.
I think that probably the error would come here. This code is on def createWidgetsMain, on the main file, and the SerialFrame class.
my_image = customtkinter.CTkImage(light_image=Image.open("images/refresh.png"),
dark_image=Image.open("images/refresh.png"),
size=(20, 20))
# CREATE REFRESH BUTTON
refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
text="")
I think that when i press the go_back button, on the other classes, it should create a new object of SerialFrame class and place in the root.
Obviously, when i create the other frames, i always send the root, the Tk().
Here is the code of the button to go create the other classes (it's inside the createWidgedsMain method):
def segmented_button_callback(value):
if value == "Inputs":
self.destroy()
input_frame.InputFrame(self.master, back).place(x=75, y=75)
if value == "Menu":
try:
connection = back.get_connection()
self.destroy()
menu_frame.MenuFrame(self.master, back).place(x=25, y=75)
except:
self.destroy()
SerialFrame(self.master).place(x=25, y=50)
segemented_button = customtkinter.CTkSegmentedButton(master=self,
values=["Menu", "Inputs"],
command=segmented_button_callback)
All application works well, my only problem is that, thank you.
Here are some pics of the app
Since you have the following code inside main.py:
...
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
So when the line from main import SerialFrame inside go_back() is executed, another instance of customtkinter.CTk() will be created which causes the exception.
You need to put the code block inside a if __name__ == "__main__" block as below:
...
if __name__ == "__main__":
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
Then the code block will not be executed when main is imported.
r_login = Tk()
r_login.title("Admin Login - Student Management System")
r_login.geometry("1280x720")
bg2 = PhotoImage(file="002.png")
lbl_bg2 = Label(r_login, image=bg2)
lbl_bg2.pack()
icon = PhotoImage(file='logo.png')
r_login.iconphoto(True, icon)
root = Tk()
root.title("Student Management System")
root.geometry("1280x720")
bg1 = PhotoImage(file="003.png")
lbl_bg1 = Label(root, image=bg1)
lbl_bg1.pack()
icon = PhotoImage(file='logo.png')
root.iconphoto(True, icon)
title = Label(root, text="Student Management System",
font=("Arial", 48, "bold"),
fg="black", bg="white")
title.place(x=226, y=30)
Output:
Traceback (most recent call last):
File "D:\Students Management\main.py", line 137, in <module>
lbl_bg1 = Label(root, image=bg1)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\amicr\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3214, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\amicr\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2628, in __init__
self.tk.call(
_tkinter.TclError: image "pyimage3" doesn't exist
I'm not being able to display the image in next. The first one is for admin for login and where the second is for another
Both of this image in my working directory and logo also. But some how I'm getting this error
You have more than one instance of Tk. All of the images are being created in the first instance and cannot be used in the second or subsequent instances.
You should not be creating two instances of Tk. Instead, if you need multiple windows. The second and subsequent windows should be instances of Toplevel.
For more info for why multiple instances of Tk is discouraged, read this
I am trying to display an image in a GUI, and don't understand what is wrong. I keep getting this error:
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
What should my code (especially the line with my_img=...) look like?
My Code:
from tkinter import *
from PIL import ImageTk,Image
my_img = ImageTk.PhotoImage(Image.open("iu.jpeg"))
my_label = Label(image=my_img)
my_label.pack()
root = Tk()
root.title("ICON PRACTICE")
root.iconbitmap('iu.ico')
button_quit = Button(root, text = "EXIT", command=root.quit)
button_quit.pack()
root.mainloop()
The full error
Traceback (most recent call last):
File "main.py", line 4, in <module>
my_img = ImageTk.PhotoImage(Image.open("test.png"))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 112, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 3997, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x7f7148fadc10>
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Try doing this, its probably because you create the root object after opening the image and creating the photo object.
import os
from tkinter import *
from PIL import ImageTk,Image
root= Tk()
i = Image.open("C:/path/to/the/image/directory/image.png")
photo = ImageTk.PhotoImage(i)
root.mainloop()
The Label widget will only work after root = Tk() (Tk() starts the underlying Tcl interpreter) is declared. Then, all child widgets must have root as their first parameter (e.g., Label(root, text='hi')). You started the interpreter after you tried to use it, so Python raised an exception.
I am not familiar with the ways of python, i saw few other questions here with similar description, but could not fix this.
Error:
Traceback (most recent call last):
File "C:/Users/UT/PycharmProjects/tkinter/python_PET/main.py", line 16, in <module>
m = menu_bar_class(root)
File "C:/Users/UT/PycharmProjects/tkinter/python_PET/main.py", line 14, in __init__
self.master.config(self.menu)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1326, in configure
return self._configure('configure', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1312, in _configure
cnf = _cnfmerge(cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 114, in _cnfmerge
for c in _flatten(cnfs):
AttributeError: Menu instance has no attribute '__len__'
Program:
from Tkinter import *
from tkFileDialog import *
import tkMessageBox
import ttk
root = Tk()
class menu_bar_class:
def __init__(self,master):
self.master = master
print("menu bar")
self.menu = Menu(self.master)
self.master.config(self.menu)
m = menu_bar_class(root)
root.mainloop()
You need to pass in the menu as a keyword argument:
self.master.config(menu=self.menu)
When you pass in a positional argument (so without the menu= part), then Tkinter expects to receive either a dictionary with configuration (so {'menu': self.menu}) or a sequence containing more sequences or dictionaries. Because self.menu is neither, you get the error you see.
hey guys i stuck on simple python GUI program
import Tkinter as tk
window = tk.Tk
text_box = tk.Entry(window)
def save_text():
str1 = text_box.get()
fx = open("file1.txt", "w")
fx.write(str1)
fx.close()
btn1 = tk.Button(window, text="Save", command="save_text")
text_box.pack()
btn1.pack()
window.mainloop()
i this error:
Traceback (most recent call last):
File "C:/Users/Saket/PycharmProjects/guiform1/firstform.py", line 5, in <module>
text_box = tk.Entry(window)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2385, in __init__
Widget.__init__(self, master, 'entry', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
self.tk = master.tk
AttributeError: class Tk has no attribute 'tk'
Anybody having idea what am i doing wrong please help me??
You have to call tk.Tk to create an instance:
window = tk.Tk()
^^
You will then have a tk.Tk-type object to interact with.