Tkinter“地方"几何管理器不工作 [英] Tkinter "place" geometry manager not working

查看:30
本文介绍了Tkinter“地方"几何管理器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从一本电子书中学习 Python.现在我正在学习 Tkinter 模块

I've been learning Python from an e-book. Right now I am learning about the Tkinter module

本书建议运行以下代码.但是,它不能正常工作.任何想法为什么?

The book suggested running the following code. However, it does not work as it should. Any ideas why?

from Tkinter import *

window = Tk()
window.geometry("200x200")

my_frame = Frame()
my_frame.pack

button1 = Button(my_frame, text = "I am at (100x150)")
button1.place(x=100, y=150)

button2 = Button(my_frame, text = "I am at (0 x 0)")
button2.place(x=0, y=0, width=100, height=50)

window.mainloop()

我应该得到什么:

我得到了什么:

添加 button1.pack()button2.pack() 后,我得到这个:

After adding button1.pack() and button2.pack(), I get this:

推荐答案

为了让您的代码正常工作,我可以做出的最小更改如下:

The smallest change I could make to make your code work is like so:

如果您要使用框架,则需要为其指定如下大小:

If you are going to use the Frame, you need to give it a size like so:

from Tkinter import *

window = Tk()
window.geometry("300x300")

# Note the change to this line
my_frame = Frame(window, width=300, height=300) 
my_frame.pack() # Note the parentheses added here

button1 = Button(my_frame, text="I am at (100x150)")
button1.place(x=100, y=150)

button2 = Button(my_frame, text="I am at (0 x 0)")
button2.place(x=0, y=0, width=100, height=50)

window.mainloop()

另外,pack()必须是函数调用,所以加括号

Also, the pack() must be a function call, so add parentheses

这篇关于Tkinter“地方"几何管理器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆