gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:43:17 26 4
gpt4 key购买 nike

我正在编写我的第一个编程代码。我想知道我是否可以在框架内添加文本和图像标签。我创建了一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com