Tkinter“地方"几何管理器不工作

编程入门 行业动态 更新时间:2024-10-25 08:25:14
本文介绍了Tkinter“地方"几何管理器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我一直在从一本电子书中学习 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:09:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   几何   地方   工作   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!