I am trying to make a program that shows you what did you do and gives an opportunity to save your deeds and remind them. I did the getting info part and write it to a txt file but taking the info from it and printing in the Text section is what couldn't I do.
This is my code.
from tkinter import *
window = tkinter.Tk()
window.geometry("500x500")
window.title('Hatırlatıcı')
def write():
text = et.get()
file_one = open('jobs.txt', 'a')
file_one.write('{}'.format(text))
file_one.write('\n')
file_one.close()
def read():
file_open = open('jobs.txt', 'r')
if file_open.mode == 'r':
contents = file_open.read()
tarea.insert(contents)
file_open.close()
def al():
write()
read()
lb1 = Label(window, text='What Did You Do?', fg='red', font=("Times", 14,
"bold"), cursor='tcross', justify='center')
et = Entry(font=("Comic Sans MS", 10, "bold"))
b1 = Button(text='Confirm', command=al)
tarea = Text(width='50')
lb1.pack()
et.pack()
b1.pack()
tarea.pack()
et.place(x='30',y='65')
b1.place(x='220',y='65')
tarea.place(x='45',y='150')
window.mainloop()
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Anaconda\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-22-4e2d8f6740e1>", line 22, in al
read()
File "<ipython-input-22-4e2d8f6740e1>", line 17, in read
tarea.insert(contents)
TypeError: insert() missing 1 required positional argument: 'chars'
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Anaconda\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-22-4e2d8f6740e1>", line 22, in al
read()
File "<ipython-input-22-4e2d8f6740e1>", line 17, in read
tarea.insert(contents)
TypeError: insert() missing 1 required positional argument: 'chars'
As the insert documentation clearly covers, the method requires two arguments: the index (a Text-form index) and text to insert. For instance
tarea.insert(INSERT, contents)
will insert at the front. See here for more details.
Related
I am trying to make a Tkinter Python CPS Test program, but it keeps returning an error that my variables time and count are being referenced before assignment. Why does this happen? I shall put my entire code up and also the error message. Help!
from tkinter import *
from time import sleep
tab = Tk()
tab.geometry('500x500')
count = 0
time = 0
def countTime():
sleep(1)
time = time + 1
tab.resizable(0, 0)
tab.title("")
def click():
count = count + 1
if time == 10:
cps = count/time
Label(tab, text = str(cps) + " clicks per second!", font = "helvetica 30 bold underline italic", padx = 50, pady = 50).pack()
btn = Button(tab, text = 'Click Me!', font = "helvetica 24 bold", command = click, background = "blue", activebackground = "yellow").pack()
for i in range(0, 10):
countTime()
And here is the error:
Traceback (most recent call last):
File "main.py", line 19, in <module>
btn = Button(tab, text = 'Click Me!', font = "helvetica 24 bold", command = click, background = "royal blue1", activ
Traceback (most recent call last):
File "main.py", line 21, in <module>
countTime()
File "main.py", line 9, in countTime
time += 1
UnboundLocalError: local variable 'time' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 14, in click
count += 1
UnboundLocalError: local variable 'count' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 14, in click
count += 1
UnboundLocalError: local variable 'count' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 14, in click
count += 1
UnboundLocalError: local variable 'count' referenced before assignment
Traceback (most recent call last):
File "main.py", line 19, in <module>
btn = Button(tab, text = 'Click Me!', font = "helvetica 24 bold", command = click, background = "royal blue1", activ
Traceback (most recent call last):
File "main.py", line 21, in <module>
countTime()
File "main.py", line 9, in countTime
time += 1
UnboundLocalError: local variable 'time' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 14, in click
count += 1
UnboundLocalError: local variable 'count' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 14, in click
count += 1
UnboundLocalError: local variable 'count' referenced before assignment
Exception in Tkinter callback
Traceback (most recent call last):
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 14, in click
count += 1
UnboundLocalError: local variable 'count' referenced before assignment
Traceback (most recent call last):
File "main.py", line 21, in <module>
countTime()
File "main.py", line 9, in countTime
time = time + 1
UnboundLocalError: local variable 'time' referenced before assignment
Also, I want to make the button change color on click also, so maybe you could help out with that too? I would prefer if every possible error would be pointed out so I could know in advance. Thanks!
this is my code:
self.msg_entry = Entry(bottom_label, bg="#2C3E50", fg=TEXT_COLOR, font=FONT)
self.msg_entry.place(relwidth=0.74, relheight=0.06, rely=0.008, relx=0.011)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
def _on_enter_pressed(self, event):
msg = self.msg_entry.get()
self._insert_message(msg, "You")
on hovering on on_enter_pressed function, it's showing function is not accessed and I'm getting the following error:
Traceback (most recent call last):
File "GUI.py", line 91, in <module>
app = ChatApplication()
File "GUI.py", line 15, in __init__
self._setup_main_window()
File "GUI.py", line 76, in _setup_main_window
self.msg_entry.bind("<Return>", self._on_enter_pressed)
AttributeError: 'ChatApplication' object has no attribute '_on_enter_pressed'
(I'm using tkinter in python to implement GUI.)
How can I solve this?
You have wrong indentations - _on_enter_pressed has to be outside __init__
def __init__(self):
# ... code ..
self.msg_entry = Entry(bottom_label, bg="#2C3E50", fg=TEXT_COLOR, font=FONT)
self.msg_entry.place(relwidth=0.74, relheight=0.06, rely=0.008, relx=0.011)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
# outside `__init__`
def _on_enter_pressed(self, event):
msg = self.msg_entry.get()
self._insert_message(msg, "You")
I want to make the user open a file using askopenfile() and assign the image to a variable
code:
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
root = Tk()
root.title("Forms")
def new_window():
root.filename = filedialog.askopenfilename(title="Select a file", filetypes=[("Png Files", "*.png")])
print(root.filename)
img = ImageTk.PhotoImage(open(root.filename))
btn = Button(text="Click here to open file .", command=new_window).pack()
root.mainloop()
But i get an error.
The output is:
C:\Users\Imtiaz\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Imtiaz/PycharmProjects/pythonProject/mbox.py
C:/Users/Imtiaz/Pictures/Roblox/RobloxScreenShot20201102_204504924.png
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Imtiaz\pyver\py390\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "C:\Users\Imtiaz\PycharmProjects\pythonProject\mbox.py", line 11, in new_window
img = ImageTk.PhotoImage(open(root.filename))
File "C:\Users\Imtiaz\PycharmProjects\pythonProject\venv\lib\site-packages\PIL\ImageTk.py", line 108, in __init__
mode = Image.getmodebase(mode)
File "C:\Users\Imtiaz\PycharmProjects\pythonProject\venv\lib\site-packages\PIL\Image.py", line 300, in getmodebase
return ImageMode.getmode(mode).basemode
File "C:\Users\Imtiaz\PycharmProjects\pythonProject\venv\lib\site-packages\PIL\ImageMode.py", line 64, in getmode
return _modes[mode]
KeyError: <_io.TextIOWrapper name='C:/Users/Imtiaz/Pictures/Roblox/RobloxScreenShot20201102_204504924.png' mode='r' encoding='cp1252'>
Answer: Thanks to acw1668 I got what the problem was.
error was in:
img = ImageTk.PhotoImage(open(root.filename))
it is supposed to be:
Either ImageTk.PhotoImage(file=root.filename) or ImageTk.PhotoImage(Image.open(root.filename))
I am trying to create a tkinter label that changes color when clicked to show that it has been visited. I keep getting an attribute error saying that Show_Label has no attribute 'fg'. Please help! Here is the code being used.
class Sheet_Label(Label):
def __init__(self,master,text):
Label.__init__(self,master,text=text,cursor="hand2",font="Times 16 underline",fg="blue")
def button_click(event):
if self.fg =="blue":
self.fg = "purple"
else:
self.fg = "purple"
location = os.getcwd()
file = webbrowser.open_new(location + '\\' + "hello.txt")
self.bind("<Button-1>",func=button_click)
def sheets_view():
sheets_window = Toplevel(window)
hello = modules.Sheet_Label(master=sheets_window,text="Hello")
hello.pack(padx=10,pady=10)
sheets_window.title("Production Sheets")
sheets_window.focus()
x = (screen_width/2) - (500/2)
y = (screen_height/2) - (500/2)
sheets_window.geometry("%dx%d+%d+%d" % (500,500,x,y))
sheets_window.resizable(0,0)
Here is the error message:
Traceback (most recent call last):
File "C:\Users\napaf\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "inventory.py", line 311, in sheets_view
hello = modules.Sheet_Label(master=sheets_window,text="Hello")
File "C:\Users\napaf\Documents\Programming\adco_project\modules.py", line 24, in __init__
self.action = action
NameError: name 'action' is not defined
PS C:\Users\napaf\Documents\Programming\adco_project> python inventory.pyException in Tkinter callback
Traceback (most recent call last):
File "C:\Users\napaf\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\napaf\Documents\Programming\adco_project\modules.py", line 27, in button_click
if self.fg =="blue":
AttributeError: 'Sheet_Label' object has no attribute 'fg'
You aren't initializing self.fg until button_click has been called, but at that point it's too late because you're trying to reference self.fg before setting it.
Also, self.fg is not the same as the fg attribute when you create the widget (eg: Label(..., fg="blue"). If you want to get the value of the widget attribute you should use self.cget('fg') or use the shortcut self['fg']. If you want to set it from within the class itself you should use self.configure(fg="purple").
Code:
import tkinter
window = tkinter.Tk()
c = tkinter.Canvas(window, height=400, width=600, bg='green')
mid_x = 600 / 2
mid_y = 400 / 2
window.geometry("600x400")
window.title("window")
def ship_control(event):
pass
c.bind_all('<KeyPress-a>', ship_control)
def start():
global ship
rock_speed = 10
ship = c.create_oval(mid_x, mid_y, mid_x + 20, mid_y + 20, fill='red', outline='orange')
button_start = tkinter.Button(window, text='play', command=start)
button_start.pack()
c.pack()
Error:
>>>
>>> Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python 3.5\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Program Files\Python 3.5\lib\queue.py", line 172, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
ValueError: invalid literal for int() with base 10: '??'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python 3.5\lib\tkinter\__init__.py", line 1534, in __call__
args = self.subst(*args)
File "C:\Program Files\Python 3.5\lib\tkinter\__init__.py", line 1252, in _substitute
e.num = getint_event(b)
File "C:\Program Files\Python 3.5\lib\tkinter\__init__.py", line 1231, in getint_event
return int(s)
SystemError: result with error in PyObject_Call
How to reproduce:
Open the program, press play then press a.
I have no idea what could be wrong altough I have tried searching on google.
I tried adding prints but they don't get executed.
Does anyone know why I get the error?
The problem was that I was using 3.5 instead of 3.4. Switching versions worked.