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

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

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!")

Related

why I get an error on Thread-1 when running my python program from an .exe file with a Tkinter GUI?

I design a GUI with Tkinter and also I add a function to convert a .docx file to .PDF file with the docx2pdf library.
I execute the main function of my python code in a Thread because when I run my code from the .exe file the GUI is locked (like it freezes).
I add an event to a button to run the main function in a new thread.
import tkinter
from tkinter import ttk
from threading import *
from docx2pdf import convert
def mProcess():
input = "C:\\test.docx"
output = "C:\\file.pdf"
convert(input,output)
def mainTask():
t = Thread(target=mProcess)
t.daemon = True
t.start()
### GUI
win = Tk()
ttk.Button(win, text = 'Start', command = mainTask).pack(pady=20)
win.mainloop()
If I run this code from my VS code it runs well. The problem only happens when I generate the .exe file of my code and run it.
I generate the exe file with pyinstaller test.py --onefile --windowed.
The error I get when run from the exe file:
Exception in thread Thread-1:
Traceback (most recent call last):
File "win32com\client\dynamic.py", line 89, in _GetGoodDispatch
pywintypes.como_error: (-2147221008, 'CoInitialize has not been called.', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "threading.py", line 973, in bootstrap_inner
File "threading.py", line 910, in run

Weird error I receive from Tkinter in Python

I am attempting to make a tkinter project but as I tried to run for like the third time it gives me this error:
Traceback (most recent call last):
File "/Users/cool/Documents/STM Wisepay Service.py", line 63, in <module>
app = App(root)
File "/Users/cool/Documents/STM Wisepay Service.py", line 20, in __init__
self.create_buttons()
File "/Users/cool/Documents/STM Wisepay Service.py", line 30, in create_buttons
tk.Button(button_frame, text = "Add to Debt", commmand = self.debt).grid(column = 6, row = 5)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2366, in __init__
Widget.__init__(self, master, 'button', cnf, kw
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf)
_tkinter.TclError: unknown option "-commmand"
What can I do to fix this? What are the problems with my code?
Here is my code:
https://pastebin.com/mWPEFbpz
You are spelling command wrong (three m's)
You are passing it a variable called commmand and it doesn't know how to use that variable.
tk.Button(... commmand = ...
You can see it in the error message right here
_tkinter.TclError: unknown option "-commmand"
These python error messages are actually very helpful. I would encourage you to read them carefully to catch stuff like this.

Python Tkinter Dynamic Scrollbar Resizing Issue

I write on Python an GUI with dynamic resizing, I mean, if the height of all the elements of the window are more than the height of the window, the scrollbar appears on the right side, but if I resize the window and the height of all elements are less than the window, the scrollbar disappear.
I have made this using the <Configure> event. This is part of the code I have used:
mycanvas = Canvas(self.parent,bd=0,highlightthickness=0,relief='flat')
sbar = tk.Scrollbar(mycanvas,orient='vertical')
sbar.config(command=mycanvas.yview)
mycanvas.config(yscrollcommand=sbar.set)
def canvasscroll(event):
mycanvas.yview('scroll',int(-1*(event.delta/120)), "units")
def resizecanvas(event):
if self.mainyposition > event.height: #mainyposition is the addition of the height of all widgets in screen
sbar.pack(side=RIGHT, fill=Y)
else:
sbar.forget()
print(event.height)
self.widgetname.bind_all("<MouseWheel>", canvasscroll)
self.widgetname.bind_all("<Configure>", resizecanvas)
Everything works well except for two issues:
First of all, when the window starts, the <Configure> event is called a few times, and print the height on screen, but the last time its called before the program stills on stand by is a height that is not the correct height of the window, so this causes that it shows unnecesarily the scrollbar. The problem solves itself once I resize the window and the event runs again, but the first run it fails doing this. Any way to solve this?
And second, during the resizing, the console doesn't show any errors, but when I close the window, it shows me an Exception in Tkinter Callback that references the pack() method of the scrollbar. I don't know how to solve this... The errors are this:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Y:\Factory\GLP2-e Admin (Todos los archivos)\Nueva Interfaz\classmain.py", line 108, in resizecanvas
sbar.pack(side=RIGHT, fill=Y)
File "C:\Python34\lib\tkinter\__init__.py", line 2140, in pack_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!canvas.!scrollbar"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Y:\Factory\GLP2-e Admin (Todos los archivos)\Nueva Interfaz\classmain.py", line 108, in resizecanvas
sbar.pack(side=RIGHT, fill=Y)
File "C:\Python34\lib\tkinter\__init__.py", line 2140, in pack_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!canvas.!scrollbar"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Y:\Factory\GLP2-e Admin (Todos los archivos)\Nueva Interfaz\classmain.py", line 108, in resizecanvas
sbar.pack(side=RIGHT, fill=Y)
File "C:\Python34\lib\tkinter\__init__.py", line 2140, in pack_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!canvas.!scrollbar"
[Finished in 22.3s]
Thanks to everyone.

python bind not working

I have the basic screensaver program. I want to bind Mouse button 1 to delete everything on screen, but when I press the Button-1 I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
TypeError: delete() takes 0 positional arguments but 1 was given
.
.
.
.
This is my program:
import tkinter
canvas=tkinter.Canvas(width=1000,height=800)
canvas.pack()
from random import *
entry1=tkinter.Entry()
entry1.pack()
def setric():
x=randrange(900)
y=randrange(700)
global a
canvas.delete('all')
farba=choice(("red","purple","blue","lightblue","green","lightgreen"))
if a>-90:
a=a-10
else:
a=-10
if entry1.get()=="":
canvas.update()
canvas.after(3000,setric)
a=0
else:
canvas.create_text(x,y,text=entry1.get(),font="Arial 40",fill=farba,angle=a)
canvas.after(2000, setric)
def delete():
canvas.delete("all")
a=0
setric()
canvas.bind('<Button-1>',delete)
Even when I change what is in the def delete I still get the error.
Thanks for your time! I am new to this so I have no idea where is the problem
....................................................................................................................................................................................................................................................
Functions called via bindings are automatically passed an object that represents the event. Your delete method needs to be able to accept this parameter even if you don't use it.
def delete(event):
canvas.delete("all")

pandasDataFrame.to_clipboard() error

I have this code to enable CTRL+C copy on the selected contents of a tkinter.ttk treeview:
def Copy(event):
'''
Copies the selected treeview items to the
clipboard. Includes Column numbers.
'''
selectedRows = []
selectedRows.append(self.maindata.columns)
for i in self.tree.selection():
selectedRows.append(self.tree.item(i)['values'])
d = pd.DataFrame(selectedRows)
d.to_clipboard(index=False)
self.tree.bind("<Control-Key-c>", Copy)
This was working fine in plan text.py files executed in IDLE but I'm using the Liclipse IDE for a more confortable debugging experience now and I get the error:
Exception in Tkinter callback
Traceback (most recent call last):
File "E:\Programming\Anaconda3_4.2.0\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "E:\Programming\EclipseWorkspace\AssetManager\workingTreeview.py", line 90, in Copy
d.to_clipboard(index=False)
File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\pandas\core\generic.py", line 1202, in to_clipboard
clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)
File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\pandas\io\clipboard.py", line 98, in to_clipboard
clipboard_set(objstr)
File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\pandas\util\clipboard.py", line 85, in _copyWindows
ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text)
OSError: exception: access violation writing 0x0000000000000000
Can anyone explain why this is happening and what I need to do to fix it?

Categories

Resources