在tkinter窗口的左上角添加一个Logo图像,与标题在同一行。

huangapple go评论93阅读模式
英文:

Adding a Logo image on top left of tkinter window along with the title in same line

问题

以下是代码部分的翻译:

  1. def __init__(self, root):
  2. self.root = root
  3. self.root.title("Data Wizard: Automated Data Preprocessing")
  4. self.root.geometry("800x600")
  5. self.root.configure(bg='#005792')
  6. # 顶部布局,包括标题
  7. self.title_label = tk.Label(self.root, text="Data Wizard: Automated Data Preprocessing", font=("Times New Roman", 22))
  8. self.title_label.pack(pady=20)
  9. self.title_label.configure(bg='#005792', fg='#7dd8c5')
  10. # 中部布局,包括表格框架
  11. self.table_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
  12. self.table_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)
  13. # 创建一个用于显示表格的treeview部件
  14. self.tree = ttk.Treeview(self.table_frame, selectmode='browse')
  15. self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  16. # 为treeview部件添加滚动条
  17. scrollbar = ttk.Scrollbar(self.table_frame, orient="vertical", command=self.tree.yview)
  18. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  19. self.tree.configure(yscrollcommand=scrollbar.set)
  20. # 底部布局,包括上传CSV文件标签和按钮
  21. self.bottom_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
  22. self.bottom_frame.pack(fill=tk.X, padx=20, pady=10)
  23. self.bottom_frame.configure(bg='#C3C3C3')
  24. self.upload_label = tk.Label(self.bottom_frame, text="Upload CSV File", font=("Arial", 12))
  25. self.upload_label.pack(side=tk.LEFT, padx=10, pady=10)
  26. self.upload_label.configure(bg='#C3C3C3', fg='#0b1956')
  27. self.upload_button = tk.Button(self.bottom_frame, text="Browse", command=self.upload_csv)
  28. self.upload_button.pack(side=tk.RIGHT, padx=10, pady=10)
  29. self.new_frame = tk.Frame(self.root, bd=0, relief=tk.GROOVE)
  30. self.new_frame.pack(fill=tk.X, padx=20, pady=10)
  31. self.new_frame.configure(bg='#005792')
  32. # 添加打开新窗口的按钮
  33. self.new_window_button = tk.Button(self.new_frame, text="Next", command=self.open_new_window)
  34. self.new_window_button.pack(side=tk.RIGHT, padx=10, pady=10)

希望这个翻译对你有所帮助。如果你有其他问题,请随时提出。

英文:

I have this logo below and I have to add it in the tkinter window, the logo shall be aligned to top left of window but in same line as the title label.

在tkinter窗口的左上角添加一个Logo图像,与标题在同一行。

  1. def __init__(self, root):
  2. self.root = root
  3. self.root.title("Data Wizard: Automated Data Preprocessing")
  4. self.root.geometry("800x600")
  5. self.root.configure(bg='#005792')
  6. # Top layout with title
  7. self.title_label = tk.Label(self.root, text="Data Wizard: Automated Data Preprocessing", font=("Times New Roman", 22))
  8. self.title_label.pack(pady=20)
  9. self.title_label.configure(bg='#005792', fg='#7dd8c5')
  10. # Middle layout with table frame
  11. self.table_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
  12. self.table_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)
  13. # Create a treeview widget to display table
  14. self.tree = ttk.Treeview(self.table_frame, selectmode='browse')
  15. self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  16. # Add scrollbar to the treeview widget
  17. scrollbar = ttk.Scrollbar(self.table_frame, orient="vertical", command=self.tree.yview)
  18. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  19. self.tree.configure(yscrollcommand=scrollbar.set)
  20. # Bottom layout with Upload CSV file label and button
  21. self.bottom_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
  22. self.bottom_frame.pack(fill=tk.X, padx=20, pady=10)
  23. self.bottom_frame.configure(bg='#C3C3C3')
  24. self.upload_label = tk.Label(self.bottom_frame, text="Upload CSV File", font=("Arial", 12))
  25. self.upload_label.pack(side=tk.LEFT, padx=10, pady=10)
  26. self.upload_label.configure(bg='#C3C3C3', fg='#0b1956')
  27. self.upload_button = tk.Button(self.bottom_frame, text="Browse", command=self.upload_csv)
  28. self.upload_button.pack(side=tk.RIGHT, padx=10, pady=10)
  29. self.new_frame = tk.Frame(self.root, bd=0, relief=tk.GROOVE)
  30. self.new_frame.pack(fill=tk.X, padx=20, pady=10)
  31. self.new_frame.configure(bg='#005792')
  32. # Add button to open a new window
  33. self.new_window_button = tk.Button(self.new_frame, text="Next", command=self.open_new_window)
  34. self.new_window_button.pack(side=tk.RIGHT, padx=10, pady=10)

The window is

在tkinter窗口的左上角添加一个Logo图像,与标题在同一行。

I also tried this following code to do so, and also the PIL methods to do this

  1. # Create a PhotoImage object from the image file
  2. my_img = tk.PhotoImage(file="C:/Users/pdhin/Minor 2/Logo_New.png")
  3. # Create the title label with the image to the left of the text
  4. self.title_label = tk.Label(self.root, text="Data Wizard: Automated Data Preprocessing", font=("Times New Roman", 22), image=my_img, compound="left")
  5. self.title_label.pack(pady=20)
  6. self.title_label.configure(bg='#005792', fg='#7dd8c5', anchor="e")

But everytime an error like this pops up

在tkinter窗口的左上角添加一个Logo图像,与标题在同一行。

答案1

得分: 1

the logo shall be aligned to top left of window but in same line as the title label.

将标志对齐到窗口的左上角,但与标题标签在同一行上。

Add self. to self.my_img = ...

self.my_img = ... 中添加 self.

英文:

> the logo shall be aligned to top left of window but in same line as
> the title label.

Add self. to self.my_img = ...

Code:

  1. class App:
  2. def __init__(self, root):
  3. ...
  4. self.my_img = tk.PhotoImage(file="p2.png")
  5. # Top layout with title
  6. self.title_label = tk.Label(self.root, text="Data Wizard: Automated Data Preprocessing",
  7. font=("Times New Roman", 22), image=self.my_img, compound="left")

Screenshot:

在tkinter窗口的左上角添加一个Logo图像,与标题在同一行。

huangapple
  • 本文由 发表于 2023年4月4日 16:25:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定