I've created a Tkinter app to convert the mailing lists we use at my company. All the functionality seems to work fine I just can't get the logo image to work!
I have created an app without using a Class and it is exactly how I'd want it.
The way I'd want the app to look:
However, I can only get the converter to work with this version using a Class:
Current working version:
The code for the working version is below (the one without any logo), I've excluded all the excel conversion code as it's quite long.
import tkinter as tk
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("SPB Mailing List Cleaner")
self.pack(fill='both', expand=1)
self.filepath = tk.StringVar()
convertButton = tk.Button(self, text='Convert',
command=self.convert, bg="#00a69d", fg="white", height="2", width="15")
convertButton.place(x=242, y=200)
filepathText = tk.Entry(self, textvariable=self.filepath)
filepathText.pack()
filepathText.place(x=237, y=250)
def convert(self):
pass # left out code
def show_file_browser(self):
self.filename = filedialog.askopenfilename()
return self.filename
def first_browser(self):
file = self.show_file_browser()
self.filepath.set(file)
form = tk.Tk()
form.geometry("600x300")
form.resizable(0, 0)
app = Window(form)
form.mainloop()
This is the code for the image in the first screenshot (the one with the logo) ('The way I'd want the app to look').
import tkinter as tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfile
from tkinter import filedialog as fd
import os
root = tk.Tk()
canvas = tk.Canvas(root, width = 600, height = 300)
canvas.grid(columnspan=3, rowspan=3)
#logo
logo = Image.open('logo.png')
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image = logo)
logo_label.image = logo
logo_label.grid(column=1, row=0)
#instructions
instructions = tk.Label(root, text="Select an appropriate '.xlsx' file for cleaning.")
instructions.grid(columnspan=3, column=0, row=1)
def open_file():
browse_text.set("loading...")
file = askopenfile(initialdir=os.path.normpath("C://"), parent=root, mode='rb', title="Choose a file", filetypes=[("Excel files", ".xlsx .xls")])
if file:
print(file.name)
#browse button
browse_text = tk.StringVar()
browse_btn = tk.Button(root, textvariable=browse_text, command=lambda:open_file(), bg="#00a69d", fg="white", height="2", width="15")
browse_text.set("Select File")
browse_btn.grid(column=1, row=2)
canvas = tk.Canvas(root, width = 600, height = 150)
canvas.grid(columnspan=3)
root.mainloop()
The question is how do I get the logo to work with the version that doesn't have a logo, i.e make the image work within a class.
I would really appreciate any feedback/help on this. I had a look at some posts that describe a similar issue but I'm quite new to coding so can't wrap my head around it all.
CodePudding user response:
I found a solution to your problem using the Pillow libery. Since your code is incomplete i can only give you a basic example on how to write the code:
# Import required libraries
from tkinter import *
from PIL import ImageTk, Image
# Create an instance of tkinter window
win = Tk()
# Define the geometry of the window
win.geometry("700x500")
frame = Frame(win, width=600, height=400)
frame.pack()
frame.place(anchor='center', relx=0.5, rely=0.5)
# Create an object of tkinter ImageTk
img = ImageTk.PhotoImage(Image.open("forest.jpg"))
# Create a Label Widget to display the text or Image
label = Label(frame, image = img)
label.pack()
win.mainloop()
Make sure that the image (forest.jpg) is located in the same folder as the script. I hope that helped :)
CodePudding user response:
First of all you havnt loaded the image in class.
but if have added then the solution is to use to make the image variable a class variable by adding self.
so final thing would look like self.img_var_name=image
.
Alternatively you can append the images in a list, that would do the trick too.