所以我通过遵循一个非常基本的教程来学习 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)
忽略我所有的蹩脚评论,但是当我在 IDLE 中运行它时,它只是卡住了,我必须使用 xkill 来关闭它。
注释掉 mLabel_4 后,IDLE 不会崩溃。发生了什么?
最佳答案
我认为主要问题是您在同一个容器中混合了不同的几何管理器。虽然这有时确实有效,但最好避免它并坚持使用 .pack()、.grid() 或 .place()。因为 tkinter 会尝试一次做所有事情并且经常失败。
明确地给你的标签 parent 也是个好主意。例如:
label = Label(mGui, text = text)
label.grid()
否则小部件将默认放置在最近提到的容器中
关于ubuntu - 在 Tkinter 中使用行/列时,IDLE 在 Elementary OS 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507997/