簡體   English   中英

如何修復python tkinter StringVar的這個錯誤?

[英]How to fix this error of python tkinter StringVar?

所以基本上我將編寫一個 python tkinter 程序來顯示重量,但是變量和對象讓我分心。

我的代碼是這樣的:

from tkinter import *
import tkinter as tk

#object oriented BMI project
class Root(Tk):
    def __init__(self): 
        super(Root, self).__init__()


        #Info Bar
        self.title("Shushu BMI Calculator")
        self.geometry("1100x600")
        #self.resizable("False")
    
        #var
        self.weight = tk.StringVar()
        #self.w = tk.StringVar()

        #Caption
        self.caption = tk.Label(text = "BMI Calculator - Beta 2.0",
                             fg = "brown",
                             font="Arial 40")
        self.caption.grid(column = 0, row = 0, sticky = "N")

        #Copyright
        self.copyright = tk.Label(text = "Powered by Shushu Studio",
                                   fg = "green",
                                   font = "Arial 20")
        self.copyright.grid(column = 1, row = 0, sticky = "E")
    
        #Weight input Row
        self.weightInputTag = tk.Label(text="Please input your weight here (kg)")
        self.weightInputTag.grid(column = 0, row = 1, sticky = "W")

        self.weightEntry = tk.Entry(textvariable = self.weight)
        self.weightEntry.grid(column = 0, row = 1)
    
        self.weightSubmit = tk.Button(text="Submit",
                                     COMMAND=self.weightget)
        self.weightSubmit.grid(column = 0, row = 2)

        self.showWeight = tk.Label(text="")
        self.showWeight.grid(column = 0, row = 3)

        def weightget(self):
            weight = self.weightEntry.get()
            self.showWeight.configure(text=weight)


root = Root()
root.mainloop()

控制台顯示:

Resetting Python state.
Running P:\2020\09\25\bmi.py
The interactive Python process has exited.
Traceback (most recent call last):
  File "P:\2020\09\25\bmi.py", line 52, in <module>
    root = Root()
  File "P:\2020\09\25\bmi.py", line 41, in __init__
    COMMAND=self.weightget)
  File "P:\Language\lib\tkinter\__init__.py", line 2345, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'weightget'

請幫忙,非常感謝!

你函數的縮進是錯誤的。

def weightget(self):
    weight = self.weightEntry.get()
    self.showWeight.configure(text=weight)

如果您希望此函數作為類方法工作, 而不是作為本地命名空間的普通函數 我認為,既然你把參數self放在函數中,那么你需要通過indentation確保它在正確的位置。

import tkinter as tk
class Root(tk.Tk):
    def __init__(self): 
        super(Root, self).__init__()


        #Info Bar
        self.title("Shushu BMI Calculator")
        self.geometry("1100x600")
        #self.resizable("False")
    
        #var
        self.weight = tk.StringVar()
        #self.w = tk.StringVar()

        #Caption
        self.caption = tk.Label(text = "BMI Calculator - Beta 2.0",
                             fg = "brown",
                             font="Arial 40")
        self.caption.grid(column = 0, row = 0, sticky = "N")

        #Copyright
        self.copyright = tk.Label(text = "Powered by Shushu Studio",
                                   fg = "green",
                                   font = "Arial 20")
        self.copyright.grid(column = 1, row = 0, sticky = "E")
    
        #Weight input Row
        self.weightInputTag = tk.Label(text="Please input your weight here (kg)")
        self.weightInputTag.grid(column = 0, row = 1, sticky = "W")

        self.weightEntry = tk.Entry(textvariable = self.weight)
        self.weightEntry.grid(column = 0, row = 1)
    
        self.weightSubmit = tk.Button(self,text="Submit",
                                      command=self.weightget)
        self.weightSubmit.grid(column = 0, row = 2)

        self.showWeight = tk.Label(text="")
        self.showWeight.grid(column = 0, row = 3)

    def weightget(self):
        weight = self.weightEntry.get()
        self.showWeight.configure(text=weight)


root = Root()
root.mainloop()
from tkinter import *
import tkinter as tk

#object oriented BMI project
class Root(Tk):
    def __init__(self): 
        super(Root, self).__init__()

        def weightget(self):
            weight = self.weightEntry.get()
            self.showWeight.configure(text=weight)

        #Info Bar
        self.title("Shushu BMI Calculator")
        self.geometry("1100x600")
        #self.resizable("False")
    
        #var
        self.weight = tk.StringVar()
        #self.w = tk.StringVar()

        #Caption
        self.caption = tk.Label(text = "BMI Calculator - Beta 2.0",
                             fg = "brown",
                             font="Arial 40")
        self.caption.grid(column = 0, row = 0, sticky = "N")

        #Copyright
        self.copyright = tk.Label(text = "Powered by Shushu Studio",
                                   fg = "green",
                                   font = "Arial 20")
        self.copyright.grid(column = 1, row = 0, sticky = "E")
    
        #Weight input Row
        self.weightInputTag = tk.Label(text="Please input your weight here (kg)")
        self.weightInputTag.grid(column = 0, row = 1, sticky = "W")

        self.weightEntry = tk.Entry(textvariable = self.weight)
        self.weightEntry.grid(column = 0, row = 1)
    
        self.weightSubmit = tk.Button(text="Submit", command=lambda:weightget(self))
        self.weightSubmit.grid(column = 0, row = 2)

        self.showWeight = tk.Label(text="")
        self.showWeight.grid(column = 0, row = 3)

        


root = Root()
root.mainloop()

這工作正常。 在過程之前定義函數。

self.weightSubmit = tk.Button(text="Submit", command=lambda:weightget(self))

當您使用面向對象編程時,像這樣為按鈕發出命令。 確保使用lambda

閱讀此https://www.w3schools.com/python/python_lambda.asp ,您將更好地了解lambda

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM