Using Tkinter,How can I toggle between images when a button is click. Having this code as reference, I can only load one image but I don't know how to make it behave the way I need.
from Tkinter import *
import ttk
from PIL import ImageTk, Image
def showImage(*args):
lbl['image'] = image_tk
root = Tk()
c = ttk.Frame(root, padding=(5, 5, 12, 0))
c.grid(column=0, row=0, sticky=(N,W,E,S))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0,weight=1)
fname = "A.jpg"
fname1 = "B.jpg"
image_tk = ImageTk.PhotoImage(Image.open(fname))
btn = ttk.Button(c, text="load image", command=showImage)
lbl1 = ttk.Label(c)
btn.grid(column=0, row=0, sticky=N, pady=5, padx=5)
lbl.grid(column=1, row=1, sticky=N, pady=5, padx=5)
root.mainloop()
How can I configure my ShowImage function or any other modification needed to be able to switch the image between fname and fname1
To change images on button clicks use the configure() method to the button to change the command argument and create a new ImageTk object to hold a reference of the second image.
from Tkinter import *
import ttk
from PIL import ImageTk, Image
def showImage():
lbl1.configure(image=image_tk)
btn.configure(text = "load image!", command=showImage1)
def showImage1():
lbl1.configure(image=image_tk1)
btn.configure(text = "load image!", command=showImage)
root = Tk()
c = ttk.Frame(root, padding=(5, 5, 12, 0))
c.grid(column=0, row=0, sticky=(N,W,E,S))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0,weight=1)
fname = "a.jpg"
image_tk = ImageTk.PhotoImage(Image.open(fname))
fname1 = "b.jpg"
image_tk1 = ImageTk.PhotoImage(Image.open(fname1)) # new image object
btn = ttk.Button(c, text="load image", command=showImage)
lbl1 = ttk.Label(c)
btn.grid(column=0, row=0, sticky=N, pady=5, padx=5)
lbl1.grid(column=1, row=1, sticky=N, pady=5, padx=5)
root.mainloop()
Related
Here is the code:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import ttk
win = Tk()
win.title("Hello python")
win.configure(bg="#010523")
#win.iconbitmap('logo.ico')
win.geometry("500x500")
main_frame = LabelFrame(win, bg="#010523", border=0)
main_frame.grid(row=1, column=0, padx=30, pady=20)
# frame_1
frame1 = LabelFrame(main_frame, bg="#1f243f", borderwidth=0)
frame1.grid(row=0, column=0, columnspan=2, sticky='w', pady=10)
text = Label(frame1, text="0013A20041EFD12C",font="10", bg="#1f243f", fg="grey")
text.grid(row=0, column=0)
text2 = Label(frame1,text="Hello everyone!",font="10", bg="#1f243f", fg="white")
text2.grid(row=1, column=0)
win.mainloop()
I want to make my frame border rounded like border-radius but don't know how to do it.
Here's the code I am using (one example):
import os
import win32api
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import ImageTk, Image
#...
def mainloop():
root = Tk()
mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0,weight=1)
#...
pillarImg = ImageTk.PhotoImage(Image.open("pillars.png"))
pillars_label = ttk.Label(root)
pillars_label['image'] = pillarImg
#...
rldir_entry = ttk.Entry(mainframe, width=40, textvariable=rldir)
pillars_entry = ttk.Entry(mainframe, width=10, textvariable=Pillars)
#...
rldir_entry.grid(column=0, row=0, columnspan=5, sticky=(W,E))
pillars_label.grid(column=0, row=1, columnspan=2, sticky=(N,W,E,S))
pillars_entry.grid(column=0, row=2, sticky=(W,E))
#...
root.mainloop()
mainloop()
This code results in the two entry boxes being above the image, even though I set the grid to have the image between the two entry boxes...?
Here's a pic of what I mean:
Ahh I just figured it out.
here:
pillars_label = ttk.Label(root)
I added the label to root.
While here:
rldir_entry = ttk.Entry(mainframe, width=40, textvariable=rldir)
the entry boxes are in the mainframe. lol.
I am creating a login system with Tkinter and the grid() method but I have no idea where I can put the image. As i did not use classes and functions, it was pretty easy to embed the path for the image (img = PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png") img1 = img.subsample(10,10), but, since I am new in Python, I donĀ“t really know where to put the path in this code when the code is more organized. Here is what I tried:
from tkinter import *
from tkinter.ttk import *
class Login_system(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Login System Prova")
Label(text = "Surname").grid(row=0, column=0, sticky=W, pady=2)
Label(text = "Your Password").grid(row=1, column=0, sticky=W, pady=2)
Label(text = "Your E-Mail").grid(row=2, column=0,sticky=W, pady=2)
Entry().grid(row = 0, column=1, pady=2)
Entry().grid(row = 1, column=1, pady=2)
Entry().grid(row = 2, column=1, pady=2)
Entry().grid(row = 3, column=1, pady=2)
Checkbutton(text = "Keep me on-line").grid(row = 4, sticky = W, columnspan= 1)
def main():
root = Tk()
root.geometry("200x150+400+300")
root.resizable(True, True)
global image
image = Frame.PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png")
app = Login_system(root)
root.mainloop()
if __name__ == "__main__":
main()
But I get this error:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\Python\GUI APP\login_system_new.py", line 40, in <module>
main()
File "C:\Users\admin\Desktop\Python\GUI APP\login_system_new.py", line 34, in main
image = Frame.PhotoImage(file = r"C:\\Users\\admin\\Desktop\\Foto_Marco\\Haken.png")
AttributeError: type object 'Frame' has no attribute 'PhotoImage'
[Finished in 0.5s]
Have you got any suggestion? I would like to put the image on the far right column.
If the image is part of the Login_system, then it is better put it inside the class. Also you forget to specify the parent of widgets inside the class, so the widgets will be children of root instead.
Also avoid importing modules like below:
from tkinter import *
from tkinter.ttk import *
In this case, you cannot use some of the widgets from tkinter because they are override by those from ttk.
Below is a modified sample based on your code:
import tkinter as tk
from tkinter import ttk
class Login_system(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
self.initUI()
def initUI(self):
self.master.title("Login System Prova")
ttk.Label(self, text="Surname").grid(row=0, column=0, sticky=tk.W, pady=2)
ttk.Label(self, text="Your Password").grid(row=1, column=0, sticky=tk.W, pady=2)
ttk.Label(self, text="Your E-Mail").grid(row=2, column=0, sticky=tk.W, pady=2)
ttk.Entry(self).grid(row=0, column=1, pady=2)
ttk.Entry(self).grid(row=1, column=1, pady=2)
ttk.Entry(self).grid(row=2, column=1, pady=2)
ttk.Entry(self).grid(row=3, column=1, pady=2)
ttk.Checkbutton(self, text="Keep me on-line").grid(row=4, sticky=tk.W, columnspan=2)
self.image = tk.PhotoImage(file=r"C:\\Users\\admin\\Desktop\\Foto\\Haken.png").subsample(10,10)
ttk.Label(self, image=self.image).grid(row=0, column=2, rowspan=5, padx=(20,0))
def main():
root = tk.Tk()
#root.geometry("200x150+400+300")
#root.resizable(True, True)
app = Login_system(root)
app.pack(fill='both', expand=1, padx=10, pady=10)
root.mainloop()
if __name__ == "__main__":
main()
I am trying to learn Tkinter and python. How can I open multiple images and save it on multiple canvas in Python 3? I also want the image to fit just perfect on to the size of the canvas.
Could someone show me to do this using simple code.
import tkinter
from tkinter import filedialog
import os
#from PIL import ImageTk, Image
from tkinter import *
import PIL.Image
from tkinter.filedialog import askopenfilename
import py_compile
mainWindow =tkinter.Tk()
mainWindow.title("Image")
mainWindow.geometry('640x480+800+200')
mainWindow.columnconfigure(0, weight=3)
mainWindow.columnconfigure(1, weight=1)
mainWindow.columnconfigure(2, weight=1)
mainWindow.rowconfigure(0, weight=3)
mainWindow.rowconfigure(1, weight=3)
mainWindow.rowconfigure(2, weight=5)
mainWindow.rowconfigure(3, weight=3)
leftFrame = tkinter.LabelFrame(mainWindow, text='PICTURE')
leftFrame.grid()
canvas = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas.grid(row=1, column=0)
canvas2 = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas2.grid(row=2, column=0)
canvas3 = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas3.grid(row=1, column=1)
canvas4 = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas4.grid(row=2, column=1)
def clicked():
print('hello')
open_img()
def open_img():
global photo
filename = filedialog.askopenfilename(initialdir = "E:/Images", filetypes = ())
photo = tkinter.PhotoImage(file=filename)
photo = photo.subsample(3,3)
canvas.create_image(0,0, anchor=CENTER, image=photo)
rightFrame = tkinter.LabelFrame(mainWindow, text='MENU')
rightFrame.grid()
button1 = tkinter.Button(rightFrame , text="Open", command=clicked)
button1.grid(row=1, column=2)
mainWindow.mainloop()
You would use one class with each image creating an instance of that class, but since you have not used a class and so are probably not familiar with the class structure, the following code combines everything into one class. Since I don't have the images, this opens a new Toplevel with a related "close" button instead of an image, each time the "open" button is clicked.
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
from functools import partial
class OpenToplevels():
""" open and close additional Toplevels with a button
"""
def __init__(self):
self.root = tk.Tk()
self.button_ctr=0
but=tk.Button(self.root, text="Open a Toplevel",
command=self.open_another)
but.grid(row=0, column=0)
tk.Button(self.root, text="Exit Tkinter", bg="red",
command=self.root.quit).grid(row=1, column=0, sticky="we")
self.root.mainloop()
def close_it(self, id):
id.destroy()
def open_another(self):
self.button_ctr += 1
id = tk.Toplevel(self.root)
id.title("Toplevel #%d" % (self.button_ctr))
tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
command=partial(self.close_it, id),
bg="orange", width=20).grid(row=1, column=0)
Ot=OpenToplevels()
I'm using rows to layout my Tkinter program.
I usually use pack so my buttons would automatically place itself and not over lap, using rows, my buttons are overlapping, and I have to align them by changing their padx value. I was wondering if there was an easier way to do this.
from sys import argv
from Tkinter import *
from PIL import Image, ImageTk, ImageFilter
import random
script, infile = argv
class MyApp(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("ImagePro")
# Original
original = Image.open(infile)
(w, h) = (original.size[0], original.size[1])
tkpi = ImageTk.PhotoImage(original)
label = Label(self.root, image=tkpi)
label.grid(row =0, column=0, padx=5,pady=5)
img = original.copy().convert("L")
tkpi2 = ImageTk.PhotoImage(img)
label = Label(self.root, image=tkpi2)
label.grid(row =0, column=1, padx=5,pady=5)
Label(self.root, text = "Original").grid(row=1, column=0)
Label(self.root, text = "Modified").grid(row=1, column=1)
Button(self.root, text = "Brighten").grid(row=2, column=0, sticky=W)
Button(self.root, text = "Darken").grid(row=2, column=0, sticky=W, padx=60)
Button(self.root, text = "Warm").grid(row=2, column=0, sticky=W, padx=112)
Button(self.root, text = "Cool").grid(row=2, column=0, sticky=W, padx=158)
self.root.mainloop()
MyApp()
To do this you need to start using frames. A frame acts as a container for widgets. Add all the buttons to a frame, then add that frame to the root container.
class MyApp(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("ImagePro")
#Original
original = Image.open(infile)
(w, h) = (original.size[0], original.size[1])
tkpi = ImageTk.PhotoImage(original)
label = Label(self.root, image=tkpi)
label.grid(row =0, column=0, padx=5,pady=5)
img = original.copy().convert("L")
tkpi2 = ImageTk.PhotoImage(img)
label = Label(self.root, image=tkpi2)
label.grid(row =0, column=1, padx=5,pady=5)
Label(self.root, text = "Original").grid(row=1, column=0)
Label(self.root, text = "Modified").grid(row=1, column=1)
self.buttonframe = Frame(self.root)
self.buttonframe.grid(row=2, column=0, columnspan=2)
Button(self.buttonframe, text = "Brighten").grid(row=0, column=0)
Button(self.buttonframe, text = "Darken").grid(row=0, column=1)
Button(self.buttonframe, text = "Warm").grid(row=0, column=2)
Button(self.buttonframe, text = "Cool").grid(row=0, column=3)
self.root.mainloop()
Each button goes in its own column within the frame, and the frame goes at the bottom of the main container.
If you use the same row,column couple for several widgets, they will appear in the same cell of the grid. Thus something like this should do the trick.
Button(self.root, text = "Brighten").grid(row=2, column=0)
Button(self.root, text = "Darken").grid(row=2, column=1)
Button(self.root, text = "Warm").grid(row=2, column=2)
Button(self.root, text = "Cool").grid(row=2, column=3)
You will find detailed information on this documentation of grid geometry manager http://effbot.org/tkinterbook/grid.htm