I want to have the button's image cycle through all possible pizza images, but instead it throws the following error when I click on it:
self.config(image=next(self.images))
AttributeError: 'buttonInitialize' object has no attribute 'config'
Here's the code. I know it's ugly, sorry. I barely understand python classes, so I couldn't figure out how to factor the repetitive parts out
from tkinter import *
from itertools import cycle
class buttonInitialize:
def __init__ (self,enabler):
frame=Frame(enabler)
frame.pack()
pizzaList=[]
pizzaList.append(PhotoImage(file="Cheese.png").zoom(10))
pizzaList.append(PhotoImage(file="AvocadoWSauce.png").zoom(10))
pizzaList.append(PhotoImage(file="AvocadoWCheese.png").zoom(10))
self.images=cycle(pizzaList)
self.printButton=Button(frame,image=pizzaList[0] ,command=self.nextPizza)
self.printButton.pack(side=LEFT)
def nextPizza(self):
self.config(image=next(self.images))
root=Tk()
c=buttonInitialize(root)
root.mainloop()
Instead of self.config(), use self.printButton.config()
Related
I am practicing on my personal project to create oop from tkinter module. I have written a code block where I try to create Label widget by imperative code line, then within a class.
from tkinter import *
app=Tk()
app.geometry('500x300')
Button(app,width=13,height=1,text='scrape').pack()
var_str=StringVar()
var_str='result 001 ...'
Label(app,width=33,height=1,text='res',textvariable=var_str).pack()
class label():
def __init__(self,master,var_text):
self.label=Label(master,width=33,height=1,textvariable=var_text).pack()
lbl_one=label(app,var_str)
app.mainloop()
The strangeness is if I comment out Label(app,width=33,height=1,text='res',textvariable=var_str).pack() then my object instantiation does not work out.
I would like to have a clear answer why lbl_one object gives same text result as with Label(app...) line?
because you probably forgot that u put StringVar at top then changed it to string which not work.
var_str=StringVar(master=app, value="res")
replace that line to this and comment the first label's line then the object would work fine.
it doesn't work
i want it to get a number then click those button bellow and then get the result in the message box what should i do?!
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
win=Tk()
#here is my problem
def household():
global s
global math
math="multiply"
x=int(E.get())
s=(x/100)*500
b="your bill is:"+str(s)
messagebox.showinfo("result",b)
def commercial():
global s
x=int(E.get())
if x<=4000000:
s=(x/100)*750
household()
commercial()
E=Entry(win,bg="#87CEFA")
b1=Button(win,text="Household",bg="#4169E1",command=household)
b3=Button(win,text="commercial",bg="#4169E1",command=commercial)
E.place(x=100,y=85,width=100,height=20)
b1.place(x=100,y=165,width=100,height=30)
b3.place(x=150,y=165,width=100,height=30)
You're calling household() and commercial() before you define E. Because you are doing a wildcard import (from tkinter import *) you're importing the constant E from tkinter, which is defined as the string "e".
The solution is:
don't do wildcard imports import tkinter with import tkinter as tk and then use tk. as a prefix for all tkinter objects (tk.Entry(...), tk.Button(...), etc)
Make sure your code runs in the correct order. If you have functions that depend on a variable, make sure those functions aren't called before the variable is defined.
I don't understand what the problem is with my code - it has everything i need and should execute fine.
No error is generated so I believe it is a logic error but I don't know how to fix it per say.
Help with this would be much appreciated.
from tkinter import *
from tkinter import ttk
def Payment_Computation(self):
def Getting_Payment_in_Monthly():
def __init__(self):
You are missing some vital parts of an oop program:
from tkinter import *
from tkinter import ttk
class MainApplication(): # create class
def __init__(self):
# method code
def Payment_Computation(self):
# method code
def Getting_Payment_in_Monthly(self, Amount_Loan, mon_rate_interest, no_of_yrs):
# method code
if __name__ == "__main__":
MainApplication() # Instantiate class
You need to place the code in a class. You then need to instantiate the class as exemplified above.
Read more about oop program structure in Best way to structure a tkinter application?
You didn't call the __init__() function. Please check carefully :)
I'm using Tkinter for the GUI, and I have one problem:
I try to make a message widget, and when I write:
body = Message(top, bd = 2)
body.pack(side=RIGHT)
I get this error:
body.pack(side=RIGHT)
AttributeError: Message instance has no attribute 'pack'
I dont understand this becaue I checked in some guides and Its allowed to use 'message' this way, as seen here in the example: http://www.tutorialspoint.com/python/tk_message.htm
Is there another way to write this?
There are at least two Message classes in Tkinter. One of them comes from tkMessageBox.Message, and the other one is from Tkinter.Message. The former is a subclass of the Dialog from tkCommonDialog, and since packing a dialog is meaningless, there is no pack method for this case. The later is a Tk widget called message, which is the one you want; being a widget, it makes sense to pack it.
Your complete code mostly like have something in the form (Python 2):
from Tkinter import *
from tkMessageBox import *
The second import shadows the Message class from the first import. To use the Message class you are after, simply change the above code to:
from Tkinter import *
import tkMessageBox
Then adapt your code accordingly.
I'm missing something at a very basic level when it comes to loading an image using PIL and displaying it in a window created by Tkinter. The simplest form of what I'm trying to do is:
import Tkinter as TK
from PIL import Image, ImageTk
im = Image.open("C:\\tinycat.jpg")
tkIm = ImageTk.PhotoImage(im)
tkIm.pack()
TK.mainloop()
When I try to run the code above, I get the following:
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute
'_PhotoImage__photo'" in <bound method PhotoImage.__del__ of
<PIL.ImageTk.PhotoImage instance at 0x00C00030>> ignored
I've confirmed the file is present and can be opened in an image editor and also that it can be displayed using im.show(). What am I missing?
Tkinter has to be instantiated before you call ImageTk.PhotoImage():
TK.Tk()
It's very true what Meredith said you need to add that line for sure!
I would like to show you my image formatting and then compare it to yours and see if there any different, my code for a image is
master.image = PhotoImage(file="Banditlogo.gif")
w = Label(master, image=master.image)
w.photo = master
w.pack()
And your code is
im = Image.open("C:\\tinycat.jpg")
tkIm = ImageTk.PhotoImage(im)
tkIm.pack()
We are both using PIL with PhotoImage
I can't help wondering are both ways correct?
At this point in time I don't have enough knowledge to fully answer your PIL question but it is interesting to compare both codes as they are different. I can only suggest doing what I do when it comes to example codes people share with me, and that is "if mine don't work, try the example code and see if that fixes the code" when I find something that works I stick with it.
Would someone with more understanding of Tkinter please explane the workings of, How do I use PIL with Tkinter?
Knowledge is power so please share.