【问题标题】:python-entry widget Place change is not going to happen and Label creating problempython-entry 小部件位置更改不会发生并且标签创建问题
【发布时间】:2020-09-06 12:09:10
【问题描述】:

代码如下所示,但未重新定位入口小部件位置,标签也未创建。在输入此代码之前[e1.grid(row=1,column=1)]程序运行良好,输入代码后不工作,如何处理这个问题..

程序是

try :
import tkinter as tk # Python 3
except :
import Tkinter as tk # Python 2

def update_sum() :
# Sets the sum of values of e1 and e2 as val of e3
try :
    sum_tk.set((float(e1_tk.get().replace(' ', '')) + float(e2_tk.get().replace(' ', ''))))
except :
    pass

root.after(10, update_sum) # reschedule the event
return

root = tk.Tk()
root.geometry('850x450')

e1_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.

# Entries
e1 = tk.Entry(root, textvariable = e1_tk)
e1.grid(row=1,column=1)
e2 = tk.Entry(root, textvariable = e2_tk)
e2.grid(row=1,column=2)
e3 = tk.Entry(root, textvariable = sum_tk)
e3.grid(row=1,column=3)

e1=Label(root,text="SL")
e1.grid(row=1,column=0)

e1.pack()
e2.pack()
e3.pack()

# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.
root.after(10, update_sum)
root.mainloop()

提前致谢..

【问题讨论】:

  • 你的程序也有错误,你在标签和条目小部件中都使用了变量 e1
  • 请修正代码中的缩进
  • 我运行了你的代码,我得到了这里提到的所有 4 个小部件。不要将grid()pack() 混合使用,否则会导致此类错误。

标签: python tkinter tkinter-entry


【解决方案1】:

您对 2 个小部件使用相同的变量并在其上使用网格函数

Grid 和 pack 是 2 个内置布局管理器,包括 place

我们只能在单个元素上使用其中一个

在您的程序中,您正在打包 e1,e2,e3 并尝试为它们提供网格布局。

您还使用了两次具有不同列值的e1.grid()

try :
    import tkinter as tk # Python 3
except :
    import Tkinter as tk # Python 2

def update_sum() :
# Sets the sum of values of e1 and e2 as val of e3
    try :
        sum_tk.set((float(e1_tk.get().replace(' ', '')) + float(e2_tk.get().replace(' ', ''))))
    except :
        pass

    root.after(10, update_sum) # reschedule the event
    return

root = tk.Tk()
root.geometry('850x450')

e1_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.

# Entries
e1 = tk.Entry(root, textvariable = e1_tk)
e1.grid(row=1,column=1)
e2 = tk.Entry(root, textvariable = e2_tk)
e2.grid(row=1,column=2)
e3 = tk.Entry(root, textvariable = sum_tk)
e3.grid(row=1,column=3)

e4=tk.Label(root,text="SL")
e4.grid(row=1,column=0)



# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.
root.after(10, update_sum)
root.mainloop()

【讨论】:

  • 你能解释一下,你真正需要什么,因为你的代码中有变量错误
  • 我想创建标签并更改条目小部件位置
  • 我已经重新编写了您的代码,请尝试运行它,如果您遇到任何错误,请在此处别针,请点赞
  • 它是因为间距,python 新手?
猜你喜欢
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
相关资源
最近更新 更多