Florian König
Florian König

Reputation: 5

How to fix "'int' object is not iterable" error in tkinter

I want to make an application, that uses Bash commands, but everytime it results in Exception in Tkinter callback.

I already have tried to use subprocess popen but there it even wanted to start the commands.

from tkinter import *
import os


# The following commands gets executed, if the user is pressing the action button
def button_action():
    update_button.config(os.system('sudo -S pacman -Syu'), text="Aktualisiert!")


# create a window
fenster = Tk()
# set the title of the window
fenster.title("Befehlsammlung")


# create button and labels
update_label = Label(fenster, text="Drücke auf Aktualisieren, um alle Pakete zu updaten.",)
update_button = Button(fenster, text="Aktualisieren", command=button_action)

exit_button = Button(fenster, text="Schließen", command=fenster.quit)

# Add your components in your favourite
update_label.pack()
update_button.pack()
exit_button.pack()


# wait for input
fenster.mainloop()

I except the button changes to "Aktualisiert" and not the actual Exception error.

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 106, in _cnfmerge
    cnf.update(c)
TypeError: 'int' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/home/flozge/PycharmProjects/untitled1/GUI.py", line 7, in button_action
    update_button.config(os.system('sudo -S pacman -Syu'), text="Aktualisiert!")
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1485, in configure
    return self._configure('configure', cnf, kw)
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1469, in _configure
    cnf = _cnfmerge((cnf, kw))
  File "/usr/lib/python3.7/tkinter/__init__.py", line 109, in _cnfmerge
    for k, v in c.items():
AttributeError: 'int' object has no attribute 'items'

Process finished with exit code 0

Upvotes: 0

Views: 367

Answers (1)

VietHTran
VietHTran

Reputation: 2318

You don't need to pass the os.system('sudo -S pacman -Syu') call as an argument for the update_button.config for it to run. You just need to call it somewhere inside the button_action function, which will be triggered when "Aktalisieren" button is clicked as specified in line update_button = Button(fenster, text="Aktualisieren", command=button_action).

def button_action():
    os.system('sudo -S pacman -Syu')
    update_button.config(text="Aktualisiert!")

Upvotes: 1

Related Questions