我正在编写我的第一个编程代码。我想知道我是否可以在框架内添加文本和图像标签。我创建了一个画布,并添加了两个框架,然后尝试添加图像和文本文件(显示在画布的顶部),但文本和图片不显示。当我运行没有帧的程序时,它确实显示。这是代码:
#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()
#
我有办法解决这个问题吗?我会假设另一种方法是将图片和文字放在最上面,然后在它下面添加框架,但我不知道该怎么做。谢谢!
答案 0 :(得分:0)
我不清楚你为什么要在画布上添加帧,但是要遵循后面的陈述;
我认为另一种方法是将图片和文字放在最上面然后在它下面添加框架,但我不知道该怎么做。
以下是如何做到这一点:
pack
用于所有小部件展示位置,但您可以替换它们
使用grid
并提供相应的row
和column
值。**
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()
另一种选择是完全省略画布并使用标签将图像和文本放在框架内。 See this post on how to use image in labels
答案 1 :(得分:0)
此代码演示了如何在 Frame
中的任何图像上放置任何文本。
它的工作原理是在 Label
内创建一个 Frame
来保存您的图像和文本。
Label
要求将 compound
属性设置为底部、中心、左侧、右侧、顶部或无
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title ('iMedic')
photo = tk.PhotoImage(file = "./logo.gif")
frame = ttk.Frame(root, relief = tk.RAISED)
frame.grid(row = 0, column = 0, sticky = tk.NSEW)
label = tk.Label(
frame, image=photo, compound = tk.CENTER,
font = "Helvetica 40 bold",
foreground = "yellow", text = "Welcome")
label.grid(row = 0, column = 0, sticky = tk.NSEW)
root.mainloop()