【问题标题】:Connecting a plot to a Button within Tkinter in Python在 Python 中将绘图连接到 Tkinter 中的按钮
【发布时间】:2017-08-10 15:06:52
【问题描述】:

这是我的代码:

def draw(x,y):
x = [0, 1, 2, 3, 4, 5, 6]
y = [5, 10, 15, 20, 25, 30, 35]
print x
print y

angles = np.linspace(-np.pi, np.pi, 100)
size = 300000
plt.plot(size * np.cos(angles), size * np.sin(angles), "k")

plt.plot(x, y, 'ko', ms=3)
plt.show()



BtPlot = tk.Button(Sater, text=" Execute :\n \n \n SAVE & PLOT ",font=(None, 
20), fg= "black", bg="green", height=10,width = 15, 
command=lambda:draw(str(x.get()),str(y.get())))

BtPlot.grid(row=50, column=10, columnspan=1) 


Sater.mainloop()

我正在尝试从 Tkinter 上的按钮界面进行绘图,但每次都会收到此错误:AttributeError: 'list' object has no attribute 'get'。有人可以帮帮我吗?

【问题讨论】:

  • get() 用于未列出的输入字段。
  • 所以我不需要放 get() ?
  • 非常感谢它的工作
  • 你的错误给了你答案。 list 对象(您的 x 和 y 列表)没有属性 get() 因此您不能将 get() 与列表一起使用,因为列表不支持该功能。要从列表中获取数据,您可以使用 for 循环或索引位置。
  • 你能给我举个例子,关于我用按钮在函数外连接字符串的例子

标签: python button plot lambda tkinter


【解决方案1】:

我在上面的 cmets 中写下Sierra Mountain Tech 给出的答案,以便可以“关闭”这个问题。 Sierra Mountain Tech 的完整学分。

get() 用于输入字段而不是列表。

你的错误给了你答案。 list 对象(您的 x 和 y 列表)没有属性 get(),因此您不能将 get() 与列表一起使用,因为列表不支持该功能。要从列表中获取数据,您可以使用 for loopindex locations

【讨论】:

    【解决方案2】:

    回应您的评论:

    你能举个例子,说明我用按钮在函数外连接字符串的例子吗?

    我不是 100% 确定您的问题,但我认为您是在询问如何连接或组合您从文件中加载的数据中的值的示例。

    所以下面我给出了一些示例,说明如何操作从函数外部加载的函数内部的数据。

    对于非 OOP 示例,您通常需要在函数中使用 global 来告诉函数您尝试使用的变量位于全局命名空间中。

    我在下面有 4 个数据操作示例。看看每个,如果您有任何问题,请告诉我。

    import tkinter as tk
    
    
    root = tk.Tk()
    root.geometry("400x175")
    
    # lets say that we loaded x, y and z below from a json file.
    x = [0, 1, 2, 3, 4, 5, 6]
    y = [5, 10, 15, 20, 25, 30, 35]
    z = "123456789"
    
    lbl1 = tk.Label(root, text = "Results: ")
    lbl1.pack(side="bottom")
    
    def combine_items():
        global x
        myvar = ""
        for item in x:
            myvar += str(item)
        lbl1.config(text ="Results:\n{}".format(myvar))
    
    def add_each_item_of_y():
        global y
        myvar = 0
        for item in y:
            myvar += item
        lbl1.config(text ="Results:\n{}".format(myvar))
    
    def add_x_and_y_per_index():
        global x, y
        myvar = []
        for i in range(len(x)):
            myvar.append(x[i] + y[i])
        lbl1.config(text ="Results:\n{}".format(myvar))
    
    def convert_string_and_sum():
        global z
        myvar = 0
        for i in z:
            myvar += int(i)
        lbl1.config(text ="Results:\n{}".format(myvar))
    
    btn1 = tk.Button(root, text = "Combine each item into one string",
                     command = combine_items).pack()
    btn2 = tk.Button(root, text = "Get sum of all items in y",
                     command = add_each_item_of_y).pack()
    btn3 = tk.Button(root, text = "Add each index of x and y together",
                     command = add_x_and_y_per_index).pack()
    btn3 = tk.Button(root, text = "Conver each item of string into a number\nand then add them together",
                     command = convert_string_and_sum).pack()
    
    
    root.mainloop()
    

    【讨论】:

    • 它工作得很好,调用字符串并绘制每件事都工作得很好,关于我在代码中使用框架和网格的包,有一些如何替换包或将它与它们混合?通过更改行和列来在同一个界面上制作所有按钮,而不用打包?
    • @Sater 我只使用了 pack 来快速编写代码。你可以用网格替换包。
    • 感谢 Sierra 的帮助,这是我最后一个问题,我成功地从按钮加载了我的 txt 文件,但我正在尝试区分我应该使用哪种功能§? " def openInstruktion(): from os import startfile startfile("c:") instruktionBtn = Button(Sater, text='load excel', command = openInstruktion) instruktionBtn.grid(row=6, column=9)
    • @Sater 1st 确保将所有导入设置在程序的顶部。第二,如果您正在加载 excel 文件,您可能希望使用 openpyxl 之类的东西来读取和管理 excel 文件,因为我不相信 tkinter 默认支持 excel 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2020-10-03
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2012-05-05
    相关资源
    最近更新 更多