I try to add and image to the button, the program will run, but the button will be blank and you cannot click on it
this is my code:
from tkinter.ttk import *
from watchorbook import *
from tkinter import *
#from PIL import Image, ImageTk
import os, sys
sys.path.append(os.path.join(os.path.dirname('sources'), "images"))
text=""
def onClickLogin(username, email):
# Creating master Tkinter window
master = Toplevel()
frame = Frame(master=master)
frame.pack(fill="both", expand="yes", padx=10, pady=10)
# main frame
frame_n = Frame(master=master)
frame_n.pack(fill="both", expand="yes", padx=10, pady=10)
# canvas
Canvas = tkinter.Canvas(frame_n)
Canvas.pack(side=tkinter.LEFT, fill="both")
# scroll bar
scroll = Scrollbar(frame_n, orient="vertical", command=Canvas.yview)
scroll.pack(side=tkinter.RIGHT, fill="y")
# cavvas cinfig
Canvas.configure(yscrollcommand=scroll.set)
Canvas.bind('<Configure>', lambda e: Canvas.configure(scrollregion=Canvas.bbox("all"), width=1000))
# frame in canvas
frame_can = tkinter.Frame(Canvas)
v = StringVar(frame_can, "1")
style = Style(frame_can)
style.configure("TRadiobutton", background="white",
foreground="black", font=("arial", 10, "bold"))
values = {"Avatar": "Avatar",
"Avengers": "Avengers",
"Spiderman": "Spiderman",
"Wolf Of Wall Street": "Wolf_Of_Wall_Street",
"Avengers-Endgame": "Avengers-Endgame",
"Doctor Strange in the Multiverse of Madness": "Doctor Strange in the Multiverse of Madness",
"Thor Love and Thunder": "Thor Love and Thunder",
"K.G.F Chapter 2": "K.G.F Chapter 2",
"RRR": "RRR",
"K.G.F Chapter 1": "K.G.F Chapter 1",
"Pushpa The Rise": "Pushpa The Rise",
"Sultan": "Sultan",
"Bajirao Mastani": "Bajirao Mastani",
"Baahubali 2 The Conclusion": "Baahubali 2 The Conclusion"
}
for (text, value) in values.items():
a = text
image = PhotoImage("images/" a ".png")
Radiobutton(frame_can, text=text, variable=v, value=value,image=image).pack(side=TOP, ipady=5)
#print(text)
#print(value)
Canvas.create_window((0, 0), window=frame_can, anchor="nw")
Button(frame, text="CONFIRM", command=lambda: moviePage(username, email, str(v.get()))).pack(side=TOP,ipady=5)
master.mainloop()
CodePudding user response:
Modify your for loop to save a reference to the button, and then pack
it on the next line. You can also avoid storing text
in a separate variable since it's being passed in as a parameter anyway
for text, value in values.items():
image = PhotoImage(f'images/{text}.png')
btn = Radiobutton(frame_can, text=text, variable=v, value=value, image=image)
btn.pack(side=TOP, ipady=5)
CodePudding user response:
That is not the answer. This shows you what gui
looks like. I will delete next 72 hrs. I also comment out from watchorbook import *
Also do not have png
for image.
When you used from tkinter import *
. Don't use this tkinter.
Just remove tkinter.
Don't used capital 'C' for variable canvas = Canvas
. Use this canvas = Canvas
. You are not using Toplevel()
. You should be able to get it working
from tkinter.ttk import *
#from watchorbook import *
from tkinter import *
#from PIL import Image, ImageTk
import os, sys
sys.path.append(os.path.join(os.path.dirname('sources'), "images"))
text=""
def onClickLogin(username, email):
# Creating master Tkinter window
master = Tk()
frame = Frame(master=master)
frame.pack(fill="both", expand="yes", padx=10, pady=10)
# main frame
frame_n = Frame(master=master)
frame_n.pack(fill="both", expand="yes", padx=10, pady=10)
# canvas
canvas = Canvas(master=master)
canvas.pack(side=LEFT, fill="both")
# scroll bar
scroll = Scrollbar(frame_n, orient="vertical", command=Canvas.yview)
scroll.pack(side=RIGHT, fill="y")
# cavvas cinfig
canvas.configure(yscrollcommand=scroll.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all"), width=1000))
# frame in canvas
frame_can = Frame(canvas)
v = StringVar(frame_can, "1")
style = Style(frame_can)
style.configure("TRadiobutton", background="white",
foreground="black", font=("arial", 10, "bold"))
values = {"Avatar": "Avatar",
"Avengers": "Avengers",
"Spiderman": "Spiderman",
"Wolf Of Wall Street": "Wolf_Of_Wall_Street",
"Avengers-Endgame": "Avengers-Endgame",
"Doctor Strange in the Multiverse of Madness": "Doctor Strange in the Multiverse of Madness",
"Thor Love and Thunder": "Thor Love and Thunder",
"K.G.F Chapter 2": "K.G.F Chapter 2",
"RRR": "RRR",
"K.G.F Chapter 1": "K.G.F Chapter 1",
"Pushpa The Rise": "Pushpa The Rise",
"Sultan": "Sultan",
"Bajirao Mastani": "Bajirao Mastani",
"Baahubali 2 The Conclusion": "Baahubali 2 The Conclusion"
}
for (text, value) in values.items():
a = text
image = PhotoImage("images/" a ".png")
Radiobutton(frame_can, text=text, variable=v, value=value,image=image).pack(side=TOP, ipady=5)
#print(text)
#print(value)
canvas.create_window((0, 0), window=frame_can, anchor="nw")
Button(frame, text="CONFIRM", command=lambda: moviePage(username, email, str(v.get()))).pack(side=TOP,ipady=5)
master.mainloop()
onClickLogin('username', '[email protected]')
Result: