【问题标题】:How can i assign Entry inputs to a variable? (bad screen distance)如何将 Entry 输入分配给变量? (屏幕距离不好)
【发布时间】:2022-01-17 02:36:34
【问题描述】:
    window = tkinter.Tk()
window.geometry('700x700')


text_eingabe = Entry(window)
text_eingabe.place(x = 250, y = 550)

def coords():
    xy = int(text_eingabe.get())
    C.create_line(10,10,{xy},200, fill= 'blue')



eingabe_bestätigung = Button(window, text= 'bitte bestätigen', command= coords)
eingabe_bestätigung.place(x = 250, y= 500)

C = Canvas(window, bg = 'white',width= 300, height= 300)
C.place(x= 170, y= 100)





window.mainloop()

问题是我收到一个名为“屏幕距离差”的错误 当我试图将数字放入输入框中以将这些数字“转换”为行的变量时

【问题讨论】:

  • 请在您的问题中包含完整的错误。
  • 只需使用C.create_line(10, 10, xy, 200, fill= 'blue')。大括号放在变量周围时会创建一个set

标签: python tkinter tkinter-canvas tkinter-entry


【解决方案1】:

{xy} 创建一个set。您只想使用该变量,因此正确的代码是:

window = tkinter.Tk()
window.geometry('700x700')


text_eingabe = Entry(window)
text_eingabe.place(x = 250, y = 550)

def coords():
    xy = int(text_eingabe.get())
    C.create_line(10,10,xy,200, fill= 'blue')

eingabe_bestaetigung = Button(window, text= 'bitte bestätigen', command= coords)
eingabe_bestaetigung.place(x = 250, y= 500)

C = Canvas(window, bg = 'white',width= 300, height= 300)
C.place(x= 170, y= 100)

window.mainloop()

【讨论】:

  • 您实际上可能还想检查 text_eingabe.get() 是否实际上是一个数字,或者使用 try / except 解决此问题
  • {xy} 创建一个set。您还刚刚发布了与 OP 相同的代码。
  • 天哪,你帮了我很多。我没有太多的编码经验,并且在这个问题上坐了超过 2 天。真的谢谢你,祝你有美好的一天
  • 你能把我的答案标记为正确吗?
  • 我把开头的句子改成了set而不是dictionary
猜你喜欢
  • 2011-11-18
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多