圆圈没有出现在图像上方
这是在板上画一个圆圈的代码。 问题是第二个框架位于另一个框架之上。 我该如何解决这个问题?
思路是:
- 制作一个称为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是首先要出现在顶部的小部件。
pack
方法或任何其他几何管理器方法用于修复GUI内部的位置,因此从上面的示例中,您首先包装的小部件似乎位于顶部。
如果您编写一个小部件并且不使用任何几何管理器,则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, sofrom 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.