0

For some reason i can't get the entry from the child window. I want to get the entry from the child window and then graph a rectangle. The error that i get is: x=float(self.txtSide.get()) AttributeError: 'MainWindow' object has no attribute 'txtSide'

import tkinter as tk
import turtle
tu=turtle


class MainWindow(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Cupe",command=self.Cupe)
        self.button.pack(side="top")

    def Cupe(self):

        c = tk.Toplevel(self)
        c.wm_title("Cupe")

        lab=tk.Label(c,text="Side")
        lab.pack()

        c.txtSide=tk.Entry(c)
        c.txtSide.pack()


        button=tk.Button(c,text="Graph",command=self.graphCupe)
        button.pack(side="bottom")


    def graphCupe(self):
        x=float(self.txtSide.get())
        tu.forward(x)
        tu.left(90)
        tu.forward(x)
        tu.left(90)
        tu.forward(x)
        tu.left(90)
        tu.forward(x)

if __name__ == "__main__":
    root = tk.Tk()
    main = MainWindow(root)
    main.pack(side="top", fill="both", expand=True)
    root.mainloop()
4

1 回答 1

1

问题是selfingraphCupe指的是MainWindow实例,而不是子窗口。您需要将子窗口传递给graphCupe函数。这将是一种方法:

    def Cupe(self):
        ...
        button=tk.Button(c,text="Graph",command=lambda: self.graphCupe(c))
        button.pack(side="bottom")

    def graphCupe(self,window):
        x=float(window.txtSide.get())
        ...

现在该graphCupe函数获取它需要操作的窗口,按钮调用该函数并将其传递给它的子窗口。

于 2014-12-07T06:24:42.780 回答