python-3.x - 如何使用 tkinter 在 Python 中的框架内添加文本和图像

标签 python-3.x tkinter tkinter-canvas

我正在编写我的第一个编程代码。我想知道我是否可以在框架内添加文本和图像标签。我创建了一个 Canvas 并向其添加了两个框架,然后尝试添加图像和文本文件(显示在 Canvas 的顶部)但文本和图片不显示。当我在没有框架的情况下运行程序时,它会显示。这是代码:

#
from tkinter import *
from tkinter import ttk

root = Tk()
root.title ('iMedic')
canvas = Canvas(root, width = 1600, height = 800)


panewindow = ttk.Panedwindow(canvas, orient = VERTICAL)
panewindow.pack(fill = BOTH, expand = True)
paitents_frame = ttk.Frame(panewindow, width = 1600, height = 400, relief = RAISED)
prescription_frame = ttk.Frame(panewindow, width = 1600, height = 300, relief = RAISED)
panewindow.add(paitents_frame, weight = 1)
panewindow.add(prescription_frame, weight = 1)

canvas.grid(row = 0, column = 0)
photo = PhotoImage(file = './logo.gif')
canvas.create_image(55, 55, image=photo)
canvas.create_text(600, 155, text = 'Welcome', font = ('Helvetica', 72, 'bold'), justify = 'center', fill='blue')
canvas.update

root.mainloop()
#

有什么办法可以解决这个问题吗?我假设另一种方法是将图片和文本放在顶部,然后在其下方添加框架,但我不知道该怎么做。谢谢!

最佳答案

我不清楚为什么要向 Canvas 添加框架,但要遵循后面的声明;

I would assume another way would be to have the pic and text on top and then add frames below it but I don't know how to do it.

这里是你如何做到的:

  1. 使 panewindow 成为 root 的子节点而不是 canvas 的子节点
  2. 框架会调整大小以适合其内容,因此我在每个框架中添加了两个标签 为了使它们可见,您应该将这些标签替换为任何一个 您需要的小部件。
  3. 我使用 pack 放置所有小部件,但您可以替换它们 使用 grid 并提供适当的 rowcolumn 值。

**

from tkinter import *
from tkinter import ttk

root = Tk()
root.title ('iMedic')
canvas = Canvas(root, width = 1600, height = 250)


canvas.pack(fill = BOTH, expand = True)
photo = PhotoImage(file = './logo.gif')
canvas.create_image(55, 55, image=photo)
canvas.create_text(600, 155, text = 'Welcome', font = ('Helvetica', 72, 'bold'), justify = 'center', fill='blue')
canvas.update

# Make panewindow child of root
panewindow = ttk.Panedwindow(root, orient = VERTICAL)
panewindow.pack(fill = BOTH, expand = True)

# paitents_frame with Labels in it
paitents_frame = ttk.Frame(panewindow, width = 1600, height = 400, relief = RAISED)
paitents_label1 = Label(paitents_frame, text="Name Label")
paitents_label1.pack()
paitents_label2 = Label(paitents_frame, text="Name Label")
paitents_label2.pack()

# prescription_frame with Labels in it
prescription_frame = ttk.Frame(panewindow, width = 1600, height = 300, relief = RAISED)
prescription_label1 = Label(prescription_frame, text="Prescription Text")
prescription_label1.pack()
prescription_label2 = Label(prescription_frame, text="Prescription Text")
prescription_label2.pack()

# Add the frames to panewindow
panewindow.add(paitents_frame, weight = 1)
panewindow.add(prescription_frame, weight = 1)

root.mainloop()

另一种选择是完全不使用 Canvas ,而是使用标签将图像和文本放置在框架内。 See this post on how to use image in labels

关于python-3.x - 如何使用 tkinter 在 Python 中的框架内添加文本和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43822633/

相关文章:

python - 将列表更改为字符串

python - 你如何创建一个 Tkinter GUI 停止按钮来打破无限循环?

Python:PIL - [Errno 32] 保存 .png 时管道损坏

python-3.x - Tkinter - 将选项卡序列限制为聚焦框架

python - 无法在 tkinter 中使用 Canvas 滚动(布局位于网格系统中)?

python - 从文本 tkinter 传递值

python - 在 Python 中使用文件属性时在控制台上显示打印

python - 使用 Python 检查字节字符串是否以特殊字符结尾

python-3.x - 有没有办法在 KivyMD 中显示 pandas Dataframe?

python - tkinter LabelFrame 没有附加小部件