I'm writing a code and I'm getting this error:
this is all the nessacery code
import os
import random
from PIL import ImageTk, Image
import tkinter as tk
def controlmenu():
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
path = "C:\\Users\user\\Documents\\Codes\\Python\\beathouse\\images\\controllermapping.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
root = tk.Tk()
frame = tk.Frame(root)
root.config(bg="black")
root.title("menu")
frame.pack()
bgc = "black"
fgc = "white"
conrtols=tk.Button(frame,
fg=fgc,
bg=bgc,
text="view controls",
command=controlmenu)
conrtols.pack(side=tk.LEFT)
root.mainloop()
and this is the error
this is not the exact code but i tested this exactly and it threw this error
meaning there are the same errors
Credit to Cool Cloud.
So the code says:
root=tk.Tk()
Well it needs to say:
root=tk.TopLevel()
Related
I'm programing a background to this program and it gives a second frame when I run it. I dont know why this happens but it does. here is the program
import tkinter as tk
import os
from PIL import ImageTk, Image
parent = os.path.dirname(os.path.realpath(__file__))
assets = os.path.join(parent,"assets")
backgrounds = os.path.join(assets,"backgrounds")
print(assets)
root = tk.Toplevel()
frame = tk.Frame(root)
frame.pack()
path = os.path.join(backgrounds,"red to blue.png")
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
Ok so the answer was that I needed it to be tk.Tk() instead of tk.Toplevel() and it works now.
I am working on my tkinter project. And when i try to compile it using pyinstaller i get the error shown in picture. This wasnt happening before reseting my pc:
The code that im trying to compile:
import tkinter as tk
from tkinter import filedialog, Text, DISABLED, NORMAL, ttk
from PIL import ImageTk, Image
import pyglet,tkinter
pyglet.font.add_file('font.ttf')
arkaplan = "#000000"
koyugri = "#1c1c1c"
root = tk.Tk()
root.title(f"AcemTube Başlatılıyor...")
root.resizable(False, False)
yer = str(root.winfo_screenwidth()/2 - 350/2).split(".")[0]
yer1 = str(root.winfo_screenheight()/2 - 90/2).split(".")[0]
root.geometry(f"350x90+{yer}+{yer1}")
root.overrideredirect(1)
canvas = tk.Canvas(root, height=90, width=350, background="#23272a")
canvas.pack()
#baslikutu = tk.Frame(root, bg="#2082b2")
#baslikutu.place(relwidth=1, relheight=0.1)
baslikutu1 = tk.Frame(root, bg=arkaplan)
baslikutu1.place(relwidth=1, relheight=1)
baslik = tk.Label(baslikutu1, text=f"AcemTube", bg=arkaplan, fg="white")
baslik.config(font=("Montserrat ExtraBold", "28", "bold italic"))
baslik.place(relwidth=0.65, relheight=0.75, relx=0.299)
frm= tk.Frame()
logo = ImageTk.PhotoImage(Image.open("acemtubes.png"))
rlbl = tk.Label(baslikutu1, image=logo, bg=arkaplan ,fg="#23272a")
rlbl.place(relwidth=0.31, relheight=0.78)
s = ttk.Style()
s.theme_use("clam")
s.configure("red.Horizontal.TProgressbar", foreground='#23272a', background='#2082b2', troughcolor=koyugri, bordercolor="#2082b2", darkcolor="#2082b2", lightcolor="#2082b2")
prgs = ttk.Progressbar(baslikutu1, style="red.Horizontal.TProgressbar", orient=tk.HORIZONTAL, length=350, mode="determinate")
prgs.place(rely=0.791)
prgs.start(24)
root.mainloop()`
Thanks for reading. Im a noobie so please explain me how to solve this issue. Thanks :)
EDIT: I forgot to say that init.tcl exists in this folder. Image Of The Tcl Folder
I'm trying to get an image to display in a Tkinter Label widget. This code works inside a class in PyCharm, but doesn't get past the 'tk.Label' line in the main app. I've consulted other answers here but haven't been able to figure out why the image isn't displaying in the main app.
logo_filepath = "/Users/xxx/MASTER/pymol/Master/cache/logos/tmpbhWv2Ts.gif"
self.img = tk.PhotoImage(file = logo_filepath)
self.logo = tk.Label(self, image=self.img)
self.logo.photo = self.img
self.logo.grid(row=0, column=3, rowspan=10, columnspan=4)
It's a very simple error. Just make sure that you aren't defining self.[Your Variable] outside of a class. Because self is only available in classes. Also, Here's my code:
import Tkinter as tk
root = tk.Tk()
logo_filepath = "Your File Path..."
img = tk.PhotoImage(file = logo_filepath)
logo = tk.Label(root, image=img)
logo.photo = img
logo.grid(row=0, column=3, rowspan=10, columnspan=4)
tk.mainloop()
#import statements
from Tkinter import *
import tkMessageBox
import tkFont
from PIL import ImageTk,Image
Code to import image:
app = Tk()
app.title("Welcome")
image2 =Image.open('C:\\Users\\adminp\\Desktop\\titlepage\\front.gif')
image1 = ImageTk.PhotoImage(image2)
w = image1.width()
h = image1.height()
app.geometry('%dx%d+0+0' % (w,h))
#app.configure(background='C:\\Usfront.png')
#app.configure(background = image1)
labelText = StringVar()
labelText.set("Welcome !!!!")
#labelText.fontsize('10')
label1 = Label(app, image=image1, textvariable=labelText,
font=("Times New Roman", 24),
justify=CENTER, height=4, fg="blue")
label1.pack()
app.mainloop()
This code doesn't work. I want to import a background image.
One simple method is to use place to use an image as a background image. This is the type of thing that place is really good at doing.
For example:
background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
You can then grid or pack other widgets in the parent as normal. Just make sure you create the background label first so it has a lower stacking order.
Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:
background_label.image = background_image
A simple tkinter code for Python 3 for setting background image .
from tkinter import *
from tkinter import messagebox
top = Tk()
C = Canvas(top, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\\Users\\location\\imageName.png")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
top.mainloop
You can use this:
root.configure(background='your colour')
Example:-
import tkinter
root=tkiner.Tk()
root.configure(background='pink')
I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself.
Currently, I can display an image like so:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
However, when the user hits, say the ENTER key, I'd like to change the image.
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
# change image
root.bind("<Return>", callback)
root.mainloop()
Is this possible?
The method label.configure does work in panel.configure(image=img).
What I forgot to do was include the panel.image=img, to prevent garbage collection from deleting the image.
The following is the new version:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
img2 = ImageTk.PhotoImage(Image.open(path2))
panel.configure(image=img2)
panel.image = img2
root.bind("<Return>", callback)
root.mainloop()
The original code works because the image is stored in the global variable img.
Another option to do it.
Using object-oriented programming and with an interactive interface to update the image.
from Tkinter import *
import tkFileDialog
from tkFileDialog import askdirectory
from PIL import Image
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
w,h = 650, 650
master.minsize(width=w, height=h)
master.maxsize(width=w, height=h)
self.pack()
self.file = Button(self, text='Browse', command=self.choose)
self.choose = Label(self, text="Choose file").pack()
self.image = PhotoImage(file='cualitativa.gif')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = ifile.name
self.image2 = PhotoImage(file=path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
Replace 'cualitativa.jpg' for the default image you want to use.
Another solution that might be of help.
In my case, I had two tk.Tk() windows. When using ImageTk.PhotoImage(), the object defaults to setting its tk window to being the first one created. A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)
import tkinter as tk
from PIL import ImageTk, Image
if __name__ == '__main__':
main_window = tk.Tk()
second_window = tk.Tk()
main_canvas = Canvas(second_window)
main_canvas.pack()
filename = 'test.png'
img = Image.open(filename)
img = img.resize((300, 100), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(img, master=second_window)
logo_label = Label(master=main_canvas, image=logo)
logo_label.image = logo
logo_label.pack()
main_window.mainloop()