簡體   English   中英

如何修復 tkinter 中的“'int' object 不可迭代”錯誤

[英]How to fix “'int' object is not iterable” error in tkinter

我想制作一個使用 Bash 命令的應用程序,但每次它都會導致 Tkinter 回調異常。

我已經嘗試過使用 subprocess 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()

我除了按鈕更改為“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

您無需將os.system('sudo -S pacman -Syu')調用作為update_button.config的參數傳遞即可運行。 您只需在button_action function 中的某處調用它,當按update_button = Button(fenster, text="Aktualisieren", command=button_action)行中的指定單擊“Aktalisieren”按鈕時,將觸發該按鈕。

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

暫無
暫無

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

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