圆圈没有出现在图像上方

发布于 2025-01-20 02:23:25 字数 987 浏览 0 评论 0原文

这是在板上画一个圆圈的代码。 问题是第二个框架位于另一个框架之上。 我该如何解决这个问题?

思路是:

  • 制作一个称为frame的框架。
  • 打包
  • 制作另一个名为 iframe5 的框架
  • 打包
  • 在 iframe5 中创建一个名为 c 的卡瓦
  • 打包
  • 在卡瓦中创建一个椭圆形
  • 将图像放入第一个框架中
  • 主循环

导入所需的库

from tkinter import *
from PIL import ImageTk, Image

# Create an instance of tkinter window
win = Tk()

# Define the geometry of the window
win.geometry("480x480")

frame = Frame(win, width=480, height=480,bd = 1)
frame.pack()
#frame.place(anchor='center', relx=0.5, rely=0.5)


iframe5 = Frame(frame, bd=2, relief=RAISED)
iframe5.pack(expand=1, fill=X, pady=10, padx=5)


c = Canvas(iframe5, bg='white', width=340, height=100)
c.pack()
c.create_oval(30,30,60,60)


# Create an object of tkinter ImageTk
img = ImageTk.PhotoImage(Image.open("Board.png"))
# Create a Label Widget to display the text or Image
#c.pack()
label = Label(frame, image = img)
label.pack()



#c.pack()
win.mainloop()

This is the code that draws a circle ontop of a board.
The problem is that the second frame is going ontop of the other.
How do I fix this?

The cide is:

  • Making a Frame called frame.
  • Packing it
  • Making anither frame called iframe5
  • packing that
  • Creating a cavas in iframe5 called c
  • Packing that
  • creates an oval in the cavas
  • Puts an image in the first fram
  • Main Loop

Import required libraries

from tkinter import *
from PIL import ImageTk, Image

# Create an instance of tkinter window
win = Tk()

# Define the geometry of the window
win.geometry("480x480")

frame = Frame(win, width=480, height=480,bd = 1)
frame.pack()
#frame.place(anchor='center', relx=0.5, rely=0.5)


iframe5 = Frame(frame, bd=2, relief=RAISED)
iframe5.pack(expand=1, fill=X, pady=10, padx=5)


c = Canvas(iframe5, bg='white', width=340, height=100)
c.pack()
c.create_oval(30,30,60,60)


# Create an object of tkinter ImageTk
img = ImageTk.PhotoImage(Image.open("Board.png"))
# Create a Label Widget to display the text or Image
#c.pack()
label = Label(frame, image = img)
label.pack()



#c.pack()
win.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2025-01-27 02:23:25

解决方案是首先要出现在顶部的小部件。 pack方法或任何其他几何管理器方法用于修复GUI内部的位置,因此

label=Label(text='hello')
label2=Label(text'hello2')
label2.pack()
label.pack()

从上面的示例中,您首先包装的小部件似乎位于顶部。

如果您编写一个小部件并且不使用任何几何管理器,则TKINTER只能保存对该小部件的引用。

The solution is to first pack the widget which you want to appear at top. pack method or any other geometry manager method is used to fix the position inside the GUI, so

label=Label(text='hello')
label2=Label(text'hello2')
label2.pack()
label.pack()

from the above example, the widget you have packed first would appear to be at top.

if you write a widget and don't use any geometry manager, tkinter would only save the reference to that widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文