Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python、Tkinter库、涉及GUI的对象中的属性错误_Python_User Interface_Object_Tkinter_Attributeerror - Fatal编程技术网

Python、Tkinter库、涉及GUI的对象中的属性错误

Python、Tkinter库、涉及GUI的对象中的属性错误,python,user-interface,object,tkinter,attributeerror,Python,User Interface,Object,Tkinter,Attributeerror,我正在为类编写一个非常简单的程序,它涉及到将一个GUI滑块的数量乘以另一个GUI滑块的数量。但是,由于某种原因,当我现在运行该程序时,我得到一个AttributeError,表示“gui”对象没有属性“slider1”。有什么想法吗?代码如下: import tkinter import random class gui: def __init__(self): self.main_window = tkinter.Tk() #widgets

我正在为类编写一个非常简单的程序,它涉及到将一个GUI滑块的数量乘以另一个GUI滑块的数量。但是,由于某种原因,当我现在运行该程序时,我得到一个AttributeError,表示“gui”对象没有属性“slider1”。有什么想法吗?代码如下:

import tkinter
import random

class gui:
    def __init__(self):
       self.main_window = tkinter.Tk()

       #widgets
       self.__canvas = tkinter.Canvas(self.main_window,bg='white',width=300,height=10)

       self.label = tkinter.Label(self.main_window,text=('Product:',0))
       self.slider1 = tkinter.Scale(self.main_window,from_=0, to=12)
       self.slider2 = tkinter.Scale(self.main_window,from_=0, to=12)

       #packs
       self.__canvas.pack()

       self.label.pack(side='top')
       self.slider1.pack(side='left')
       self.slider2.pack(side='right')
       self.button = tkinter.Button(self.main_window,text='Click to multiply',command=self.multiply())
       self.button.pack(side='bottom')

       tkinter.mainloop()

   def multiply(self):
       x = int(self.slider1.get())
       y = int(self.slider2.get())
       num = x*y
       self.label.config(text=('Product:',num))

gui()

程序中有一些语法错误,我对此进行了评论。你也应该在天平上标出方位。这是代码

import tkinter as tk

class gui:
  def __init__(self):
    self.root = tk.Tk()

    # the widgets
    self.button = tk.Button(self.root, text="Multiply!", command=self.multiply)
    # you need no '()' for the function when inputing it in tkinter.
    self.label = tk.Label(self.root, text="Product: 0") # the '0 must be a string
    self.sliderX = tk.Scale(self.root, from_=0, to=12, orient=tk.HORIZONTAL)
    self.sliderY = tk.Scale(self.root, from_=0, to=12, orient=tk.VERTICAL)
    # add an orient to the scales.

    # now pack the widgets.
    self.button.pack()
    self.label.pack()
    self.sliderX.pack()
    self.sliderY.pack()

  def multiply(self):
    x = int(self.sliderX.get())
    y = int(self.sliderY.get())
    num = str(x * y) # need to turn the int to a string.
    self.label.config(text="Product: "+num)

app = gui()
app.root.mainloop()

它不适用于您的原因是因为没有该程序的实例。这就是我最后要做的。Python的垃圾收集收集使用
gui()
生成的实例,因此Tkinter不能引用该类的实例

我编辑你的问题,现在就可以了@如果不存在任何元素,ParkerCan无法处理任何元素。更改前创建/访问..非常感谢您Preston,您解决了我的问题。我喜欢你的编码风格,比我的简单整洁多了。第一行代码是否使tkinter更容易被引用为tk?是的,它导入模块,但使用不同的名称。编译器稍后会对其进行更改,但我们不必担心这一点。