Python 3.6 - _tkinter.TclError - python-3.x

Today I tried to make a text editor using Tkinter's Filedailog, Font and Text functions. But when I tried to run it, It gave me a tkinter.tclerror.
I am using Sublime Text Editor and Command Prompt with a Virtual Environment
Here is my Code:
from tkinter import *
from tkinter import filedialog
from tkinter import font
root = Tk()
root.title('CodePad Code Editor')
root,geometry("1200x660")
# Create Main Frame which will take text
my_frame = Frame(root)
my_frame.pack(pady=5)
# Create Text Box for text input
my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbakground="yellow", selectforeground="black", undo=True, yscrollcommand=text_scroll.set)
my_text.pack()
root.mainloop()
And this was the error:
Traceback (most recent call last):
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\runpy.py",
line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\runpy.py",
line 85, in _run_code
exec(code, run_globals)
File "e:\Python-Projects\text_editor\TextEditor.py", line 5, in <module>
root = Tk()
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__
init__.py", line 2017, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, want
objects, useTk, sync, use)
_tkinter.TclError
Anybody please tell me what can I do to clear the error
Thanks
Steve

Related

Kivy in Jypiter notebook: OSError: source code not available

Im̀ trying to create a code that uses kivy library to create a window that has a button as widget. To do that I used the following code copied from a online video so as far as I know it should work:
(Each running cell will be divided by some dots, as the first one runs with no problems but the second is where the error occures)
from kivy.app import App
from kivy.uix.button import Button
from kivy.interactive import InteractiveLauncher
from kivy.lang import Builder
#forzar a la app a no iniciar en pantalla completa
from kivy.config import Config
Config.set("graphics", "fullscreen", "0")
janela = None
glayout= None
...
class WindowApp(App):
pass
window = WindowApp()
wi = InteractiveLauncher(janela)
wi.run()
#the error that appears on the console after running the second cell goes like this:
[WARNING] [Deprecated ] Call to deprecated function __init__ in /home/alfred/anaconda3/envs/k39/lib/python3.10/site-packages/kivy/interactive.py line 308.Called from /tmp/ipykernel_5673/4179568724.py line 5 by <module>().
Exception in thread Thread-5 (startApp):
Traceback (most recent call last):
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
self.run()
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/site-packages/kivy/interactive.py", line 319, in startApp
app.run(*args, **kwargs)
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/site-packages/kivy/app.py", line 954, in run
self._run_prepare()
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/site-packages/kivy/app.py", line 923, in _run_prepare
self.load_kv(filename=self.kv_file)
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/site-packages/kivy/app.py", line 676, in load_kv
default_kv_directory = dirname(getfile(self.__class__))
File "/home/alfred/anaconda3/envs/k39/lib/python3.10/inspect.py", line 785, in getfile
raise OSError('source code not available')
OSError: source code not available
kvcode= """
FloatLayout:
Button:
size_hint: .1, .1
pos_hint: {"x":.3, "top": 1.}
text: "A"
"""
glayout= Builder.load_string(kvcode)
janela.root_window.add_widget(glayout)
After running the last cell a black window that crashes appears. So far Im quite new to using the Kivy library so Im̀ quite lost about what counld have gonne wrong since I've seen this code run without problems online.

Python _tkinter.TclError: bad screen distance "0.5" using Turtle module

I'm a turtle's beginner and I wrote a little program (file.py) who draws something.
But when I call the file with an import statement from another file (theotherfile.py. Note that it use tkinter), I have this error :
Traceback (most recent call last):
File "C:\Users\jeandubois\Documents\GitHub\myproject\theotherfile.py", line 443, in <module>
import theotherfile
File "C:\Users\jeandubois\Documents\GitHub\myproject\file.py", line 10, in <module>
turtle1 = turtle.Turtle()
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3813, in __init__
Turtle._screen = Screen()
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3663, in Screen
Turtle._screen = _Screen()
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3689, in __init__
self._root.setupcanvas(width, height, canvwidth, canvheight)
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 438, in setupcanvas
self._canvas = ScrolledCanvas(self, width, height, cwidth, cheight)
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 343, in __init__
self._canvas = TK.Canvas(master, width=width, height=height,
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2685, in __init__
Widget.__init__(self, master, 'canvas', cnf, kw)
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2569, in __init__
self.tk.call(
_tkinter.TclError: bad screen distance "0.5"
My code :
# file.py
import tkinter
root = tkinter.Tk()
[some awesome code]
root.mainloop()
# when closing root:
import theotherfile
# theotherfile.py
import turtle
turtle1 = turtle.Turtle()
turtle2 = turtle.Turtle()
turtle3 = turtle.Turtle()
turtle1.speed(100)
turtle2.speed(100)
turtle3.speed(100)
turtle1.width(5)
turtle2.width(5)
turtle3.width(5)
turtle1.color(choice(colors))
turtle2.color(choice(colors))
turtle3.color(choice(colors))
turtle1.up()
turtle2.up()
turtle3.up()
turtle1.goto(-100, -100)
turtle2.goto(0, -100)
turtle3.goto(-50, -13.4)
turtle1.down()
turtle2.down()
turtle3.down()
a = 100
for loop in range(2):
for loop2 in range(3):
turtle1.forward(a)
turtle1.left(120)
turtle2.forward(a)
turtle2.left(120)
turtle3.forward(a)
turtle3.left(120)
turtle1.color(choice(colors))
turtle2.color(choice(colors))
turtle3.color(choice(colors))
a = 50
turtle1.forward(50)
turtle2.forward(50)
turtle3.forward(50)
for loop in range(5):
turtle1.forward(a)
turtle1.left(120)
turtle2.forward(a)
turtle2.left(120)
turtle3.forward(a)
turtle3.left(120)
turtle1.color(choice(colors))
turtle2.color(choice(colors))
turtle3.color(choice(colors))
turtle1.right(60)
turtle2.right(60)
turtle3.right(60)
turtle1.forward(50)
turtle2.forward(50)
turtle3.forward(50)
turtle1.up()
turtle2.up()
turtle3.up()
turtle1.hideturtle()
turtle2.hideturtle()
turtle3.hideturtle()
turtle.mainloop()
I use Python 3.9.0 .
Can someone help me please ?

Ttk style layout not found how to fix?

I am making my own custom Menubar.My work is in process but i recently came across one confusion.
Here is my code:-
from tkinter import *
from tkinter.ttk import Style,Frame as fp
class menu():
def __init__(self,parent,bg="#ffffcc"):
self.parent = parent
self.bg = bg
#this is a image in base 64 encoded format
borderImageData='''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAABmJLR0QA/wD/
AP+gvaeTAAACZ0lEQVRoge2aPU/TURSHn/MvpbYYKa9qQQnlTSOJRBzoYBDi
YEwcdPFD+PIFHHRxcCXxO7gxmBgXSTcxMYQEE4spoEWIvBcDBfp2HVqlxRhK
TDwtuc947vJ7cnPvGc6R/v5+KhlHO8C/YgW0sQLaWAFtqg4WxKEjJMGQaQyK
txbHpZGqmGzG7GzKyoyZfcfsOCZbeChFjaz+vAw/oq6V1K75HiERJ5v+33H/
xKnC55czF3CfYD1mxkbY+Pb7sECguUtuPQbMh5d8eksmpZL2r7jccvEGV++B
Ma+fsRzNlwOBAID3lNx+gjHm1VNiEweuqSwwWZajxCak85oEQ3wOk0my/4j7
7uD1m7ERNuY1Ux7KeozwC3x+uXI3V3AAXFXSPcjCFAtTmuFKw8xPsviRrsHc
B+MANHVS7WPuvXK0kjGz43hqaOogL3CyASC+oJrqKMQXAWoayQmI2wuY1J5q
qKOQ2gXE4+MYdGIroI0V0MYKaGMFtLEC2lgBbayANlZAGyugjRXQxgpoYwW0
sQLaWAFtrIA2DmCSCQC3RzlL6VR7AZIJ8jewtQaIv0Uz05GoPQuYrVXyAqsz
JBO0D+imKh0JhtjbYmWGvEAmbabDtPTSelk5Wimc6yNwiekw2Qz7j3hylERc
hh9S36YZ7jCkoU2GHpjtdTM5mqv8mtSnkyxFpHtQeoZIJ1n7UnbDelc1vTfl
+n1EePOcH0u5cvGyR11r/hJSeyxFzPZGmSx7SE09p3twe1j7asZGCifCcnDt
UhzaBwgOSHMnZbNuw86mWY7K3Lg5ZN2mAjkWnbiisQLaWAFtKl7gJzIjtMOb
uqQwAAAAAElFTkSuQmCC
'''
self.borderImage = PhotoImage( data=borderImageData,master=self.parent)
self.TP_style=Style()
self.TP_style.element_create("RoundedFrame",
"image", self.borderImage,
border=14, sticky="nsew")
self.TP_style.layout("RoundedFrame",
[("RoundedFrame", {"sticky": "nsew"})])
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
self.frame_one.pack()
l1=Label(parent,image=self.borderImage).pack()
def popup(self,x,y,width=110,height=120):
self.width=width
self.height=height
self.app = Tk()
self.app.config(bg=self.bg)
self.app.geometry(f"{self.width}x{self.height}+{x}+{y}")
self.app.wm_attributes("-topmost",True)
self.app.overrideredirect(True)
self.app.focus_force()
#self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
#self.frame_one.pack()
#l1=Label(self.app,image=self.borderImage).pack()
#self.m.pack_propagate(0)
def destroy(event):
self.app.destroy()
self.app.bind("<FocusOut>",destroy)
if __name__ == "__main__":
root = Tk()
menu = menu(root)
def evt(evt):
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
root.bind("<Button-3>",evt)
root.mainloop()
If i write style for frame like
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
in the init() method every thing works fine. If same thing i write in popup methon(where i have just commented out),i got the following error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\mishra\P_Menu.py", line 64, in evt
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
File "C:\Users\mishra\P_Menu.py", line 51, in popup
self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 740, in __init__
Widget.__init__(self, master, "ttk::frame", kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: Layout RoundedFrame not found
Same thing for Lable
l1=Label(self.app,image=self.borderImage).pack()
In the init() section everything works fine but in popup method I got following error.
Traceback (most recent call last):
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\mishra\P_Menu.py", line 64, in evt
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
File "C:\Users\mishra\P_Menu.py", line 53, in popup
l1=Label(self.app,image=self.borderImage).pack()
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist
I want to know how to fix it,cause i want to know logic behind it and use in popup() method.
Any help will be appriciated.
Thank You!
Maybe it's because popup creates a new Tk instance as self.app, but the style belongs to the first Tk instance created by root = Tk(). So a child of the second Tk instance cannot recognize it. Maybe you can try changing self.app = Tk() to self.app = Toplevel() to see if it works?

Python Tkinter Error When Using MatPlotLib

Can someone help me with this error message. I am using MatPlotLib to graph something, but I get the following error message when I call the function to graph it. Tkinter Error Message
Use one of following commands are :: --
'UPLOAD' to upload a file
'DOWNLOAD' to download a file
'DELETE' to delete a file
'DIR' to view the current directories
DOWNLOAD
enter the name of the file to download!!!
break.txt
file opened
file close()
time: start = 0 end = 1587306325.2672875
time: transfer_time = 1587306325267.287
file downloaded and graph is shown below!!
Traceback (most recent call last):
File "file_client.py", line 117, in <module>
plot_graph(result)
File "file_client.py", line 81, in plot_graph
plt.scatter(list(time_dict.keys()), list(time_dict.values()))
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 3364, in scatter
ax = gca()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 962, in gca
return gcf().gca(**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 592, in gcf
return figure()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 539, in figure
**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 171, in new_figure_ manager
return cls.new_figure_manager_given_figure(num, fig)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 1049, in n ew_figure_manager_given_figure
window = Tk.Tk(className="matplotlib")
File "/usr/lib/python3.6/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useT k, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

Pyinstaller "ValueError: Can't mix absolute and relative paths"

I am using windows 10 and anaconda3 to manage my python packages. This is my first time to use python, and I'm trying to make my own gui program with pyqt5. Also I'm trying to make .exe file using Pyinstaller. The issue I am running into is that the .exe is throwing the error block:
(pyqt5_env) C:\Python Projects>pyinstaller -w -F App_ver05.py
268 INFO: PyInstaller: 4.0.dev0+b3dd91c8a8
268 INFO: Python: 3.7.7 (conda)
268 INFO: Platform: Windows-10-10.0.18362-SP0
Traceback (most recent call last):
File "c:\anaconda3\envs\pyqt5_env\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\anaconda3\envs\pyqt5_env\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Anaconda3\envs\pyqt5_env\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\__main__.py", line 112, in run
spec_file = run_makespec(**vars(args))
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\__main__.py", line 58, in run_makespec
spec_file = PyInstaller.building.makespec.main(filenames, **opts)
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\building\makespec.py", line 458, in main
specfile.write(onefiletmplt % d)
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\building\makespec.py", line 101, in __repr__
self.variable_prefix, self.filename_suffix = make_variable_path(self.path)
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\building\makespec.py", line 84, in make_variable_path
if os.path.commonpath([filename, from_path]) == from_path:
File "c:\anaconda3\envs\pyqt5_env\lib\ntpath.py", line 615, in commonpath
raise ValueError("Can't mix absolute and relative paths") from None
ValueError: Can't mix absolute and relative paths
The same error occurs regardless of which .py file is used. For information, I wrote the used code below.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn1 = QPushButton('&Button1', self)
btn1.setCheckable(True)
btn1.toggle()
vbox = QVBoxLayout()
vbox.addWidget(btn1)
self.setLayout(vbox)
self.setWindowTitle('QPushButton')
self.setGeometry(300, 300, 300, 200)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
I uninstalled and reinstalled pyinstaller but it didn't work. I don't think its a code issue because the code is really simple. Can anyone give a solution or hint for this problem?
What happened for me is that the absolute path name had a space in it. Once the project was moved to a directory without spaces it was able to build
The solution of this problem is to use the full file name and append the flag -F to the file name. In your case for example if for the file App_ver05.py the absolute path is :
/home/user123/Desktop/foldername/App_ver05.py
Then use the command :
pyinstaller -F /home/user123/Desktop/foldername/App_ver05.py

Resources