简体   繁体   中英

Python 3 and GUI Tkinter logo insertion

import tkinter as tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename, asksaveasfilename

def get(name):
  if name in imagelist:
    if imagelist[name][1] is None:
      print('loading image:', name)
      imagelist[name][1] = PhotoImage(file=imagelist[name][0])
        return imagelist[name][1]
      return None

path = '.\logo.gif'
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "right", fill = "both", expand = "yes")

def open_file():
    """Open a file for editing."""
    filepath = askopenfilename(
        filetypes=[("Python files", "*.py"), ("All Files", "*.*")]
    )
    if not filepath:
        return
    txt_edit.delete(1.0, tk.END)
    with open(filepath, "r") as input_file:
        text = input_file.read()
        txt_edit.insert(tk.END, text)
    window.title(f"Simple Text Editor - {filepath}")

def save_file():
    """Save the current file as a new file."""
    filepath = asksaveasfilename(
        defaultextension="py",
        filetypes=[("Python files", "*.py"), ("All Files", "*.*")],
    )
    if not filepath:
        return
    with open(filepath, "w") as output_file:
        text = txt_edit.get(1.0, tk.END)
        output_file.write(text)
    window.title(f"Simple Text Editor - {filepath}")

def runsomething():
          exec((open_file()).read()) 

window = tk.Tk()
window.title("Application")
window.rowconfigure(0, minsize=500, weight=1)
window.columnconfigure(2, minsize=500, weight=1)

window.configure(background='grey')

txt_edit = tk.Text(window)
fr_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
btn_open = tk.Button(fr_buttons, text="Open", command=open_file)
btn_save = tk.Button(fr_buttons, text="Save As...", command=save_file)
btn_close = tk.Button(fr_buttons, text="Close", command=quit)
btn_exec = tk.Button(fr_buttons, text="Execute", command=runsomething)

btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5)
btn_close.grid(row=2, column=0, sticky="ew", padx=5)
btn_exec.grid(row=3, column=0, sticky="ew", padx=5)
fr_buttons.grid(row=0, column=0, sticky="ns")
txt_edit.grid(row=0, column=1, sticky="nsew")

window.mainloop()

Hi all I am new in python and in tkinter. I am trying to create a small app that will have some buttons and my logo on the grey area at right corner. Everything working fine but I have problems inserting the logo on the same window. The code I have attached is creating a second window with the logo. Any suggestions? Thanks

You can get the image on the same window by removing the code that creates the new window (between def get and def open_file ) and add it to the existing window like this:

txt_edit = tk.Text(window)
...
btn_exec = tk.Button(fr_buttons, text="Execute", command=runsomething)
img = ImageTk.PhotoImage(Image.open(".\logo.gif"))
lbl_logo = tk.Label(window, image = img)

btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
...
lbl_logo.grid(row = 0, column = 2, sticky = "nesw")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM