There is a main window. In this window there is a button that, when clicked, opens a new window. In this window, you can select parameters and set them when you press a certain button. But when I press this button, an error occurs.

Unbound method get () must be called with Settings_of_generation instance as the first argument (got nothing instead)

Here is a piece of code:

class Settings_of_generation(object): def __init__(self): self = Tk() self.title('Настройки генерации') self.maxsize(500, 500) self.minsize(500, 500) self.geometry('500x500+100+100') label_length_scale_of_password = Label(self, text = 'Длина пароля:', font = 'Arial') label_length_scale_of_password.place(x = 0, y = 0) length_scale_of_password = Scale(self, orient = HORIZONTAL, length = 30, from_ = 1, to = 30, tickinterval = 1, resolution = 1) length_scale_of_password.place(x = 0, y = 20, width = 500) but = Button(self, text = 'get', command = Settings_of_generation.get()) but.pack() def get(self): label_length_scale_of_password.configure(text = length_scale_of_password.get()) 

Full code:

 # -*- coding: utf-8 -*- from Tkinter import * class Settings_of_generation(object): def __init__(self): self = Tk() self.title('Настройки генерации') self.maxsize(500, 500) self.minsize(500, 500) self.geometry('500x500+100+100') self.label_length_scale_of_password = Label(self, text = 'Длина пароля:', font = 'Arial') self.label_length_scale_of_password.place(x = 0, y = 0) self.length_scale_of_password = Scale(self, orient = HORIZONTAL, length = 30, from_ = 1, to = 30, tickinterval = 1, resolution = 1) self.length_scale_of_password.place(x = 0, y = 20, width = 500) but = Button(self, text = 'get', command = self.get()) print ('succes') but.pack() def get(self): self.label_length_scale_of_password.configure(text = self.length_scale_of_password.get()) root = Tk() root.title('Генератор пароля') root.maxsize(500, 300) root.minsize(500, 300) root.geometry('500x300+600+200') generation_settings_button = Button(root, text = 'Настройки генерации', command = Settings_of_generation) generation_settings_button.place(x = 0, y = 0) generation_button = Button(root, text = 'Сгенерировать') generation_button.place(x = 410, y = 0) root.mainloop() 
  • Maybe in the last line self.length_scale_of_password? What do you have initialized in this variable? Give more code - FeroxTL
  • self.length_scale_of‌ _password is not suitable. I understand that self is the second window. It is called by clicking on the button "generation_settings_button = Button (root, text = 'Generation Settings', command = Settings_of_generation)" - Immersion

1 answer 1

The first is that your get(self) method is an object method because you specified self as the first parameter, you can call it only on the current object instance, or by passing a reference to another object by the parameter.

Settings_of_generation.get () - so you can not write. If only to transfer some object.
Settings_of_generation (). Get () - will lead to recursion.
self.get () is probably what you wanted to do.
self.get is what you need, since the Tkinter documentation says that you need to specify the callback function in the command parameter, which will be called every time you press a button.

The second is that your label_length_scale_of_password variable label_length_scale_of_password local and is not initialized in any way in your class, which means it is None and you will call your configure(text = length_scale_of_password.get()) method configure(text = length_scale_of_password.get()) in None. This will lead to an error. If for some reason it is defined somewhere outside the class, then there is no problem.

 from Tkconstants import HORIZONTAL from Tkinter import Label, Scale, Button, Tk class Settings_of_generation(object): def __init__(self, root): self.root = root root.title('Settings generation') root.maxsize(500, 500) root.minsize(500, 500) root.geometry('500x500+100+100') root.after(500, self.update) self.label_length_scale_of_password = Label(root, text = 'Password length:', font = 'Arial') self.label_length_scale_of_password.place(x = 0, y = 0) self.length_scale_of_password = Scale(root, orient = HORIZONTAL, length = 30, from_ = 1, to = 30, tickinterval = 1, resolution = 1) self.length_scale_of_password.place(x = 0, y = 20, width = 500) but = Button(root, text='get', command=self.get) print("success") but.pack() def get(self): self.label_length_scale_of_password.configure(text=self.length_scale_of_password.get()) def update(self): self.root.update() root = Tk() Settings_of_generation(root) root.mainloop() 
  • The first is when using self.get (), this error occurs: Exception in Tkinter callback Traceback (most recent call last): File "C: \ Python 2.7.12 \ lib \ lib-tk \ Tkinter.py", line 1537, in call return self.func (* args) File "■■■■■■■■ .py", line 20, in init but = Button (self, text = 'get', command = self.get ()) File " C: \ Python 2.7.12 \ lib \ lib-tk \ Tkinter.py ", line 1899, in getattr return getattr (self.tk, attr) AttributeError: get - Immersion
  • Second, will the problem solve the following: outside the class, define a variable and assign the value "a = length_scale_of_password.get ()" to it? - Immersion
  • Previously did not work with Tkinter. Now there is no time to understand much. I will add the answer to what happened. Try running at home. - Max
  • Specifically, in your example, everything runs without errors, but initially the label does not contain the text 'Password length:', but there is a '1'. Pressing the get button does not update the text. I tried to fix it a little. My "but = Button (self, text = 'get', command = self.get ())" does not go further. Error: AttributeError: get. Sorry, of course, maybe I do not understand your answer a little and because of this I am doing wrong. At the end of the question I attached all the code. - Immersion
  • The tkinter documentation says that command accepts a callback function that will be called when pressed. I corrected the answer. effbot.org/tkinterbook/button.htm - Max