【问题标题】:IDLE is crashing on Elementary OS when using rows/columns in Tkinter在 Tkinter 中使用行/列时,IDLE 在 Elementary OS 上崩溃
【发布时间】:2013-12-11 00:08:33
【问题描述】:

所以我通过一个非常基础的教程来学习 Tkinter。到目前为止,这是我的文件:

import sys
from Tkinter import *
# Makes a variable and makes it an instance of the Tk() class
mGui = Tk()

# "500x500" is the dimensions. The other "+100+100" determines where the top left starts
mGui.geometry("500x500+100+100")

# Renames the window to "Learning GUI". Notice it isnt mGui.title = "Learning GUI"
mGui.title("Learning GUI")

# This will pack it automatically
"""mlabel = Label(text = "My Label").pack()"""

# This will pack it later, it's usually better. fg = foreground or text color in this case bg = background
# the Pack function places the object onto the center of the window.
mlabel = Label(text = "My Label 1", fg="red", bg="white")
mlabel.pack()
# Notice how it places it down under the original so they don't overlap.
# mlabel_2 = Label(text = "My Label", fg="red", bg="white")
# mlabel_2.pack()

# Here we are using place and placing it at the designated x and y values.
mlabel_2 = Label(text = "My Label 2", fg="red", bg="white")
mlabel_2.place(x=230, y=250)

#.grid is like creating a grid
mlabel_3 = Label(text = "My Label 3", fg="red", bg="white").grid(row = 0, column = 0)

mlabel_4 = Label(text = "My Label 4", fg="red", bg="white").grid(row = 1, column = 0)

忽略我所有的蹩脚的 cmets,但是当我在 IDLE 中运行它时,它只是冻结了,我必须使用 xkill 来关闭它。

注释掉 mLabel_4 后,IDLE 不会崩溃。发生了什么事?

【问题讨论】:

    标签: ubuntu python-2.7 ide python-idle


    【解决方案1】:

    我认为主要问题是您在同一个容器中混合了不同的几何管理器。虽然这有时确实有效,但最好避免它并坚持使用 .pack()、.grid() 或 .place()。因为 tkinter 会尝试一次完成所有事情,并且经常失败。

    另外,明确地给你的标签父母是个好主意。例如:

    label = Label(mGui, text = text)
    label.grid()
    

    否则小部件将默认放置在最近提到的容器中

    【讨论】:

    • 没有考虑过...混合包和网格绝对是的问题。除非您非常熟悉它们的工作原理,否则它几乎永远不会起作用。即使这样,也不推荐。
    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多