繁体   English   中英

Python 3 和 GUI Tkinter 标志插入

[英]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()

大家好,我是 python 和 tkinter 的新手。 我正在尝试创建一个小应用程序,该应用程序将在右上角的灰色区域上有一些按钮和我的徽标。 一切正常,但我在同一个 window 上插入徽标时遇到问题。 我附上的代码是创建带有徽标的第二个 window。 有什么建议么? 谢谢

您可以通过删除创建新 window 的代码(在def getdef open_file之间)并将其添加到现有的 window 中,从而在同一 window 上获取图像,如下所示:

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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