Как исправить ошибку "int" is not iterable "в tkinter - PullRequest
0 голосов
/ 29 сентября 2019

Я хочу создать приложение, которое использует команды Bash, но каждый раз это приводит к исключению в обратном вызове Tkinter.

Я уже пытался использовать подпроцесс popen, но там он даже хотел запускать команды.

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, за исключением кнопки, меняется на "Aktualisiert", а не на фактическую ошибку исключения.

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

1 Ответ

1 голос
/ 29 сентября 2019

Вам не нужно передавать вызов os.system('sudo -S pacman -Syu') в качестве аргумента для update_button.config для его запуска.Вам просто нужно вызвать его где-нибудь внутри функции button_action, которая будет срабатывать при нажатии кнопки «Aktalisieren», как указано в строке update_button = Button(fenster, text="Aktualisieren", command=button_action).

def button_action():
    os.system('sudo -S pacman -Syu')
    update_button.config(text="Aktualisiert!")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...