【问题标题】:how can i put widget next row using tkinter pack() method?如何使用 tkinter pack() 方法将小部件放在下一行?
【发布时间】:2016-04-13 03:01:39
【问题描述】:

如何使用 tkinter pack() 方法将小部件放在下一行?我用了 pack(side= LEFT) 但我不能像我上传的图片一样。包(边= LEFT)只剩下...我不能放下一行小部件。一世 想知道..

[]

from tkinter import *   
app = Tk()

app.title('')
app.geometry("800x1200")



Label(app, text = 'a:').pack(side= LEFT)
a = Entry(app)
a.insert(0, "")
a.pack(side= LEFT)


Label(app, text = 'b:').pack(side= LEFT)
b = Entry(app)
b.insert(0, "")
b.pack(side= LEFT)




Label(app, text = 'c:').pack(side= LEFT)
c = Entry(app)
c.insert(0, "")
c.pack(side= LEFT)



Label(app, text = 'd:').pack(side= LEFT)
d = Entry(app)
d.insert(0, "")
d.pack(side= LEFT)

Label(app, text = 'e:').pack(side= LEFT)
e = Entry(app)
e.insert(0, "")
e.pack(side= LEFT)





Label(app, text = 'f:').pack(side= LEFT)
f = Entry(app)
f.insert(0, "")
f.pack(side= LEFT)


Label(app, text = 'g:').pack(side= LEFT)
g = Entry(app)
g.insert(0, "")
g.pack(side= LEFT)


Label(app, text = 'h:').pack(side= LEFT)
h = Entry(app)
h.insert(0, "")
h.pack(side= LEFT)



Label(app, text = '').pack()
text = Text(app, width=100,height=12)
text.insert('1.0', "text")
text.pack()




Label(app, text = '').pack()
text2 = Text(app, width=100,height=12)
text2.insert('1.0', "text")
text2.pack()



Button(app, text = 'save ').pack()

app.mainloop()

Traceback(最近一次调用最后一次):文件“D:/34545.py”,第 2 行,在 Label(app, text = 'a:').pack(side= LEFT) NameError: name 'Label' is not defined

【问题讨论】:

  • 只是一个友好的提示,您可能需要阅读此页面:The How-To-Ask Guide,这样您就可以始终确保您的问题易于回答且尽可能清晰。请务必包括您为解决遇到的问题所做的任何努力,以及尝试这些修复时发生的情况。也不要忘记您的显示代码和任何错误消息!
  • 使用额外的框架对您的小部件进行分组。任何 tkinter 教程都涵盖了这种布局问题。阅读其中之一。此外,这很可能是一个重复的问题,SO 上已经有答案。

标签: python tkinter


【解决方案1】:

如果要使用pack,则需要将按钮 A、B、C 和 D 放在一个框架中,将按钮 E、F、G 和 H 放在另一个框架中。另一种选择是使用grid 将所有这些按钮放在一个框架中。

【讨论】:

    【解决方案2】:

    这是代码:

    from tkinter import * 
    import tkinter as tk  
    app = Tk()
    
    app.title('')
    app.geometry("800x1200")
    
    
    
    tk.Label(app, text = 'a:').pack(side= LEFT)
    a = Entry(app)
    a.insert(0, "")
    a.pack(side= LEFT)
    
    
    tk.Label(app, text = 'b:').pack(side= LEFT)
    b = Entry(app)
    b.insert(0, "")
    b.pack(side= LEFT)
    
    
    
    
    tk.Label(app, text = 'c:').pack(side= LEFT)
    c = Entry(app)
    c.insert(0, "")
    c.pack(side= LEFT)
    
    
    
    tk.Label(app, text = 'd:').pack(side= LEFT)
    d = Entry(app)
    d.insert(0, "")
    d.pack(side= LEFT)
    
    tk.Label(app, text = 'e:').pack(side= LEFT)
    e = Entry(app)
    e.insert(0, "")
    e.pack(side= LEFT)
    
    
    
    
    
    tk.Label(app, text = 'f:').pack(side= LEFT)
    f = Entry(app)
    f.insert(0, "")
    f.pack(side= LEFT)
    
    
    tk.Label(app, text = 'g:').pack(side= LEFT)
    g = Entry(app)
    g.insert(0, "")
    g.pack(side= LEFT)
    
    
    tk.Label(app, text = 'h:').pack(side= LEFT)
    h = Entry(app)
    h.insert(0, "")
    h.pack(side= LEFT)
    
    
    
    tk.Label(app, text = '').pack()
    text = Text(app, width=100,height=12)
    text.insert('1.0', "text")
    text.pack()
    
    
    
    
    tk.Label(app, text = '').pack()
    text2 = Text(app, width=100,height=12)
    text2.insert('1.0', "text")
    text2.pack()
    
    
    
    Button(app, text = 'save ').pack()
    
    app.mainloop()
    

    如果您想更改此按钮、条目或标签的位置,我认为您应该使用 place(x=?,y=?)

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多