How can I create a Text wiget in Tkinter, Python 3.8 - python-3.x

I wanted to make a text wiget in window, but saw this traceback:
Traceback (most recent call last):
File "D:/хрень на питоне/experement3.py", line 18, in <module>
vl=Text(root, width = 20, heigth = 100 , wrap = WORD)
File "C:\Users\Людмилп\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3554, in __init__
Widget.__init__(self, master, 'text', cnf, kw)
File "C:\Users\Людмилп\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-heigth"
My code looked like this:
from tkinter import *
root=Tk()
root.geometry('400x300')
app=Frame(root)
app.grid(row=2,column=0,sticky=S)
bt=Button(app,text='''
First button
''')
bt.grid()
app1=Frame(root)
app1.grid(row=2,column=1,sticky=S)
bt1=Button(app1,text='''
Second button
''')
bt1.grid()
vlf=Frame(root)
vlf.grid(row=0,column=3)
vl=Text(root, width = 20, heigth = 100 , wrap = WORD)
vl.grid()
root.mainloop()
What could went wrong?

Please read your error messages carefully because they almost ever contain useful information. In this case it tells you that in line 18 where you initialise the Text widget you give it an option "heigth" which it can't take. This is due to the fact that you just misspelled "height" by switching "t" and "h" which can happen and is just human.
PS: Your english isn't bad, don't worry. ^^

Related

TypeError: '>' not supported between instances of 'method' and 'int', but in this case the 'method' is a number

I'm new to creating videogames with python (in the porgram I am using turtle) and I got this error:
Traceback (most recent call last):
File "/home/barbieri/pong.py", line 75, in <module>
ball.setx(ball.xcor() + ball.dx) # make the ball move (xcor = current cordinate)
File "/usr/lib/python3.7/turtle.py", line 1808, in setx
self._goto(Vec2D(x, self._position[1]))
File "/usr/lib/python3.7/turtle.py", line 3158, in _goto
screen._pointlist(self.currentLineItem),
File "/usr/lib/python3.7/turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "/usr/lib/python3.7/tkinter/__init__.py", line 2469, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"
for these lines of code:
while True:
wn.update()
# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Borders
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
Can somebody help me? Python version: 3.7.3. Linux operating system.
I am following a tutorial that uses python 3.6, that could be the cause or it's the same?
I was also a beginning when I saw this tutorial. I also did the same program. This is sure to come because you paused the game on your own instead of the program ending itself. This is will come but in the terminal window and not in the main game window. This is a flaw from the developer who made this program.

Resize image in Tkinter

So, I need to resize an image in tkinter. Before you do anything - this is not a duplicate. I have gone through every other question on this site and none have helped me. The thing with me is - I don't wan't to save the image. I need to load the image, resize it, then display it with PhotoImage in a label. I tried to use ImageTk, but for some reason it won't work.
Here's my code with ImageTk:
from tkinter import *
import PIL
from PIL import Image, ImageTk
root = Tk()
left_nose_path_white = Image.open(r'C:\Users\User\Documents\Python stuff\Other apps\Veteris\Dog photos\Advanced\Sides\GIF\Nose\Nose (left) - White.gif')
def resize_image():
global left_nose_path_white
total_screen_width = root.winfo_screenwidth()
total_screen_height = root.winfo_screenheight()
frame_width, frame_height = left_nose_path_white.size
dog_new_width = int(frame_width / 3)
dog_new_height = int(frame_height / 3)
left_nose_path_white = left_nose_path_white.resize((dog_new_width, dog_new_height), Image.LANCZOS)
resize_image()
left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
label = Label(image = left_nose_img_white)
label.pack()
root.mainloop()
This returns the error:
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
My code should find the width and height of the original image, divide it by 3, and then show it. The reason I don't want to have to save the image is because the user will open the application several times.
I'm new to using PILL/Pillow, so the answer may be obvious.
I have Pillow installed using pip.
I only have one version of Python on my computer (Python 3.7)
Full Error:
Traceback (most recent call last):
File "C:\Users\User\Documents\Python stuff\Other apps\Veteris\Scripts\Veteris_program.py", line 230, in <module>
left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 95, in __init__
image = _get_image_from_kw(kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 64, in _get_image_from_kw
return Image.open(source)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2779, in open
prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x000002B0A8A49950>
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Thanks for the help!

Tkinter error messages - redrawing buttons

I write chess on Python3, use Tkinter. I have a problem with bot-mode. Player correctly play: click on buttons and figures move. After player's turn bot should play.
def main():
start_position()
root.mainloop()
while king_alive:
global bot_turn
if bot_turn:
cells = l.bot_move(log_field)
replace(cells[0], cells[1])
bot_turn = False
this is function replace() which change condition of cells on the field
def replace(first_cell, second_cell):
if second_cell.figure.type != 'nofig':
delete_fig(second_cell.figure)
first_cell.clicked = False
second_cell.clicked = False
second_cell.fig_name = first_cell.fig_name
second_cell.fig_owner = first_cell.fig_owner
second_cell.figure = second_cell.get_figure()
first_cell.figure = l.NoFigure(first_cell.x, first_cell.y, "")
first_cell.fig_name = ""
first_cell.fig_owner = ""
field[second_cell.x][second_cell.y].configure(fg=second_cell.fig_owner, text=second_cell.fig_name)
field[first_cell.x][first_cell.y].configure(text="")
demark_cells()
people can easily play and move figures, but bot can't do it, however he use the same functions of movement - replace() (input for method replace() are correct. I can't understand the traceback
Traceback (most recent call last):
File "C:/Users/Даша/Desktop/python/task chess/graphics.py", line 176, in <module>
main()
File "C:/Users/Даша/Desktop/python/task chess/graphics.py", line 171, in main
replace(cells[0], cells[1])
File "C:/Users/Даша/Desktop/python/task chess/graphics.py", line 138, in replace
field[second_cell.x][second_cell.y]['fg'] = second_cell.fig_owner
File "C:\Python34\lib\tkinter\__init__.py", line 1275, in __setitem__
self.configure({key: value})
File "C:\Python34\lib\tkinter\__init__.py", line 1268, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1259, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".36689776"
how to solve the problem?
P.S. log_field is matrix of every cell conditions
field is matrix of tkinter buttons
You have a problem in the line:
field[second_cell.x][second_cell.y]['fg'] = second_cell.fig_owner
Because you have destroyed, or assigned a different variable to a widget that is called in that line. I don't know exactly what your variables are, but it is probably field[second_cell.x][second_cell.y]['fg'] or second_cell.

tkinter picture error: "images may not be named same as on main window"

I'm trying to write a tkinter version of flappy bird, and I came across an error that I've never seen before while working with tkinter. I've searched around everywhere, and tried everything I can think of. This is my code so far:
from tkinter import *
root = Tk()
canvas = Canvas(root, height=400, width=400)
canvas.pack()
bird = PhotoImage(root, file="L:\\Programming\\Python\\flappyBird\\bird.png")
and this is the error:
Traceback (most recent call last):
File "L:\Programming\Python\flappyBird\flappyBird.py", line 5, in <module>
bird = PhotoImage(root, file="L:\\Programming\\Python\\flappyBird\\bird.png")
File "C:\Python34\lib\tkinter\__init__.py", line 3384, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python34\lib\tkinter\__init__.py", line 3340, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: images may not be named the same as the main window
>>>
Looking at the documentation for PhotoImage, it doesn't look like supplying any positional arguments is recommended. Try creating it without the root.
bird = PhotoImage(file="L:\\Programming\\Python\\flappyBird\\bird.png")
The first keyword argument in the PhotoImage constructor is name. By passing root as the first positional argument, it is being associated with the name keyword argument. Thus, you are trying to create a widget with the same name as the root window, hence the error "images may not be named the same as the main window"
Omit root as the first argument when creating the image and the problem will go away.

How to fill to lower-right corner in python-curses

I'm using curses in Python3.3 and need to fill the entire usable space with characters. The error I'm getting occurs when my double for loop reaches the last corner.
Traceback (most recent call last):
File "main.py", line 14, in <module>
curses.wrapper(main)
File "/usr/local/lib/python3.3/curses/__init__.py", line 94, in wrapper
return func(stdscr, *args, **kwds)
File "main.py", line 9, in main
stdscr.addch(y, x, ord('.'))
_curses.error: addch() returned ERR
I've noticed that each time a character is added, the cursor moves to the right. I haven't tested this as thoroughly as I might have, but I suspect that the cursor leaves the end of the window on that last call to stdscr.addch, which likely causes the error.
An additional note is that by using a maximum width or height one unit under what the window returns, I'm able to loop without an error.
Source that fails:
import curses
def main(stdscr):
height, width = stdscr.getmaxyx()
for y in range(height):
for x in range(width):
stdscr.addch(y, x, ord('.'))
stdscr.refresh()
stdscr.getch()
if __name__ == '__main__':
curses.wrapper(main)
For my project, it's important that I use a complete rectangle. Up to this point I've shortened my max width from 80 to 79, but if anyone knows a way to write to that last corner, I'd like to hear it.
Calling insch in corner instead of addch worked for me.

Resources