When I run Python script in python3 it works well but in python 2.7 I get an error and I should run this code in Python 2.7 :
this is my code:I want to test some algorithms on path-finding and want to create a window consisting of grids where I can drag my mouse while clicked to create walls and delete them with right click and drag
import Tkinter
class Cell():
FILLED_COLOR_BG = "green"
EMPTY_COLOR_BG = "white"
FILLED_COLOR_BORDER = "green"
EMPTY_COLOR_BORDER = "black"
def __init__(self, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill= False
def _switch(self):
""" Switch if the cell is filled or not. """
self.fill= not self.fill
def draw(self):
""" order to the cell to draw its representation on the canvas """
if self.master != None :
fill = Cell.FILLED_COLOR_BG
outline = Cell.FILLED_COLOR_BORDER
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
class CellGrid(Canvas):
def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs)
self.cellSize = cellSize
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize))
self.grid.append(line)
#memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
#bind click action
self.bind("<Button-1>", self.handleMouseClick)
#bind moving while clicking
self.bind("<B1-Motion>", self.handleMouseMotion)
#bind release button action - clear the memory of midified cells.
self.bind("<ButtonRelease-1>", lambda event: self.switched.clear())
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def _eventCoords(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
cell._switch()
cell.draw()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
def handleMouseMotion(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
if cell not in self.switched:
cell._switch()
cell.draw()
self.switched.append(cell)
if __name__ == "__main__" :
app = Tk()
grid = CellGrid(app, 50, 50, 10)
grid.pack()
app.mainloop()
And error:
NameError: name 'Canvas' is not defined
How can I change my code in order to runnig this in Python 2.7?
Well, it's not defined all right, I wonder how could it run on Py3, unless Tkinter there dumped Canvas into your namespace somehow.
You probably wanted to do class CellGrid(Tkinter.Canvas), but I don't remember the exact structure of Tkinter offhand, so it might be in a subpackage or something.
You need to import elements of module you want to use, in your case:
from Tkinter import Canvas, Tk
You can also do:
from Tkinter import *
but it is not recomended way of doing. Your code may not work right, if you use many modules, and they two of them happend to have same name of method.
Last way is just
import Tkinker
but then every time you use element of this module, you need to add full path so if you wanna use Canvas, you need to useTkinker.Canvas.
Also remember that in Python 2.7 you import Tkinter and in 3.X import tkinter
Related
Im working with Tkinter currently and im trying to make an draggable canvas image, the code i've "made"/edited from another stack overflow question, the second one to be particular. There code works for me when im using tk.Frame(), but it gives an error for Canvas.create_image(). So i edited it a little but now it seems to to be moving the image at all when i drag it
My code:
from tkinter import *
import pyautogui
x,y=pyautogui.size()
tk = Tk()
c = Canvas(tk, width=x, height=y)
c.place(x=-2,y=-3)
img = ImageTk.PhotoImage(PIL.Image.open(r"Data\backgrounds\background.jpg"))
c.create_image(0, 0, image=img, anchor=NW)
def command6():
print("command6")
def make_draggable(widget,btn="<Button-1>",motion="<B1-Motion>"):
def __draggable__(widget):
c.tag_bind(btn,btn,on_drag_start)
c.tag_bind(motion,motion,on_drag_motion)
def on_drag_start(event):
widget = event.widget
widget._drag_start_x = event.x
widget._drag_start_y = event.y
def on_drag_motion(event):
widget = event.widget
x = widget.winfo_x() - widget._drag_start_x + event.x
y = widget.winfo_y() - widget._drag_start_y + event.y
widget.move(widget,x, y)
__draggable__(widget)
APP_6 = r'apps\File.png'
APP_6 = PIL.Image.open(APP_6)
APP_6 = APP_6.resize((48,48),PIL.Image.ANTIALIAS)
APP_6 = ImageTk.PhotoImage(APP_6)
image_6 = ImageTk.PhotoImage(PIL.Image.open(r"apps\File.png"))
image_id_6 = c.create_image(48,48, image=APP_6)
c.move(image_id_6, 1,y-735)
c.tag_bind(image_id_6, '<Button-1>',command6)
make_draggable(image_id_6)
tk.mainloop()
this gives no errors or any unwanted output in the console.
There are few issues inside make_draggable() function:
first argument of .tag_bind() is the item ID of a canvas item. For your case, it is widget argument of make_draggable(). So the following lines:
c.tag_bind(btn,btn,on_drag_start)
c.tag_bind(motion,motion,on_drag_motion)
should be changed to
c.tag_bind(widget, btn, on_drag_start)
c.tag_bind(widget, motion, on_drag_motion)
Same apply to c.move(...) inside on_drag_motion()
the logic/calculation on moving the canvas item is wrong
Below is the modified make_draggable() function:
def make_draggable(widget, btn="<Button-1>", motion="<B1-Motion>"):
def __draggable__(widget):
c.tag_bind(widget, btn, on_drag_start)
c.tag_bind(widget, motion, on_drag_motion)
c._item_id = widget # save the item ID for later use
def on_drag_start(event):
widget = event.widget
# get the top-left coordinates of the selected item
x, y, *_ = widget.bbox(widget._item_id)
# save the offset of current mouse position from the top-left coordinates
widget._dx = event.x - x
widget._dy = event.y - y
def on_drag_motion(event):
widget = event.widget
# calculate the top-left coordinates of the item that the item to be moved to
x = event.x - widget._dx
y = event.y - widget._dy
# move the item using moveto() instead of move()
widget.moveto(widget._item_id, x, y)
__draggable__(widget)
As the above logic applies only on the item of the last call of make_draggable(). It is better to use class instead of function:
class make_draggable():
def __init__(self, item_id, btn="<Button-1>", motion="<B1-Motion>"):
self.item_id = item_id
c.tag_bind(item_id, btn, self.on_drag_start, add=True)
c.tag_bind(item_id, motion, self.on_drag_motion, add=True)
def on_drag_start(self, event):
x, y, *_ = event.widget.bbox(self.item_id)
self.dx, self.dy = event.x-x, event.y-y
def on_drag_motion(self, event):
event.widget.moveto(self.item_id, event.x-self.dx, event.y-self.dy)
I'm trying to build a chessboard consisting of buttons.
I created 3 widgets in one line. There are labels outside (filling) and inside I want to put a chessboard.
I would like it to always occupy 90% of the screen width and automatically adjust its height so that it always remains a square. It would also be necessary to set the buttons always to be squares but I also can't handle it. Can You help me?
class ChessBoard(GridLayout):
def __init__(self, **kwargs):
super(ChessBoard, self).__init__(**kwargs)
self.cols = 8
for i in range(64):
self.cell = Button(text="", size_hint_y=self.height/8, height=self.width/8)
self.add_widget(self.cell)
class ChessBoardContainer(GridLayout):
def __init__(self, **kwargs):
super(ChessBoardContainer, self).__init__(**kwargs)
self.orientation='horizontal'
self.cols=3
self.lab1 = Label(text="1")
self.add_widget(self.lab1)
self.board = ChessBoard()
self.add_widget(self.board)
self.lab2 = Label(text="2")
self.add_widget(self.lab2)
class CombWidget(BoxLayout):
def __init__(self, **kwargs):
super(CombWidget, self).__init__(**kwargs)
self.orientation='vertical'
self.but1 = Button(text="But1", font_size=40)
self.add_widget(self.but1)
self.chessb = ChessBoardContainer()
self.add_widget(self.chessb)
self.but2 = Button(text="But2", font_size=40)
self.add_widget(self.but2)
class MyPaintApp(App):
def build(self):
return CombWidget()
Right now this is my result:
I would like to get something like this (Paint master ;) ). Maybe it could be done without this labels?
To make buttons be squares you just have to set the height and width of GridLayout cells, and you are trying to do it with size_hint. Try this:
from kivy.core.window import Window
class ChessBoard(GridLayout):
def __init__(self, **kwargs):
super(ChessBoard, self).__init__(**kwargs)
self.cols = 8
winsize = Window.size
sizedict = {}
# to set width and height of GridLayout cells, you should make a dict, where the key is col's/row's number and the value is size
for i in range(self.cols):
sizedict[i] = winsize[0]/8 #or you can divide it by 10 for example to have some black filling on the sides
# and then simply do this
self.cols_minimum = sizedict
self.rows_minimum = sizedict
This code produces buttons that look fairly square to me. If you plan to use images for your chess pieces, the buttons will conform to the size of those.
from tkinter import Tk, Button
window = Tk ()
squares = []
index = 0
for x in range (8) :
for y in range (8) :
squares.append (Button (window, width = 7, height = 4))
squares [index].grid (row = x, column = y)
index += 1
window.mainloop ()
I am imaging fluorescent cells from a 384-well plate and my software spits out a formatted excel analysis of the data (16 rowsx24 columns of images turns into a list of data, with 2 measurements from each well, ~800 data points). Because there is a lot of manual interaction with the data, I want to automate my work by taking the information that's indexed in the excel sheet and map it as a tkinter grid. I want to take my data, have it formatted from a list back to the original 16x24 display. I want to be able to interact with that data, among other things, but as a newbie in python, I'm having a difficult time mapping this data.
I used pandas to read my dataframe, but can't seem to display my list to the appropriate grid box. Ideally, I'd like the values in the excel list to display on the corresponding cell in the tkinter grid. For example, in my excel list, [1]:https://imgur.com/a/N8HAtYl, the two data points from each well are listed. I want to take the values from each cell and display them to the corresponding grid in tkinter, so the average of values of "A1" would be mapped onto column 1, row 1 of the grid, A2 would be col 2 row 1, A3 would be col 3 row 1, and so on and so forth.
Any help would be great, thank you.
from tkinter import *
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
df = pd.read_excel('/Users/Zammam/PycharmProjects/Filipin_Analysis_Zammam/data.xlsx', sheet_name='Dataps')
print(()
class Cell():
FILLED_COLOR_BG = "green"
EMPTY_COLOR_BG = "white"
FILLED_COLOR_BORDER = "green"
EMPTY_COLOR_BORDER = "black"
def __init__(self, master, x, y, size):
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill= False
def _switch(self):
""" Switch if the cell is filled or not. """
self.fill= not self.fill
def draw(self):
if self.master != None :
fill = Cell.FILLED_COLOR_BG
outline = Cell.FILLED_COLOR_BORDER
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
class CellGrid(Canvas):
def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs)
self.cellSize = cellSize
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize))
self.grid.append(line)
#memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
#bind click action
self.bind("<Button-1>", self.handleMouseClick)
#bind moving while clicking
self.bind("<B1-Motion>", self.handleMouseMotion)
#bind release button action - clear the memory of midified cells.
self.bind("<ButtonRelease-1>", lambda event: self.switched.clear())
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def create_text(self):
for row in self.grid:
for cell in row:
for i in df:
cell.create_text(text = df)
def _eventCoords(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
cell._switch()
cell.draw()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
def handleMouseMotion(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
if cell not in self.switched:
cell._switch()
cell.draw()
self.switched.append(cell)
if __name__ == "__main__" :
app = Tk()
grid = CellGrid(app, 16, 24, 15)
grid.pack()
app.mainloop()
I'm not familiar with pandas so I will use openpyxl to demonstrate. It will probably be something similar with pandas or even simpler.
from openpyxl import load_workbook
import tkinter as tk
root = tk.Tk()
file = "your_excel_file.xlsx"
wb = load_workbook(file, data_only=True)
ws = wb.active
r = 0
for row in ws:
c = 0
for cell in row:
tk.Label(root,text=cell.value).grid(row=r,column=c)
c+=1
r+=1
root.mainloop()
A quick look at using pandas instead:
df = pd.read_excel('your_excel_file.xlsx',header=None)
for i, row in df.iterrows():
c = 0
for cell in row:
tk.Label(root, text=cell).grid(row=i, column=c)
c += 1
I want to test some algorithms on path-finding and want to create a window consisting of grids where I can drag my mouse while clicked to create walls and delete them with right click and drag. I just started with computer science and am fascinated with the algorithms however i don't know how to create the interactive window for it.
The example grid can be viewed here:
A* pathfindind visualisation
Here come a solution. You can use the list of list of cells objects contained in the CellGrid.grid object to apply your pathfinding algorithm on. Have fun.
from tkinter import *
class Cell():
FILLED_COLOR_BG = "green"
EMPTY_COLOR_BG = "white"
FILLED_COLOR_BORDER = "green"
EMPTY_COLOR_BORDER = "black"
def __init__(self, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill= False
def _switch(self):
""" Switch if the cell is filled or not. """
self.fill= not self.fill
def draw(self):
""" order to the cell to draw its representation on the canvas """
if self.master != None :
fill = Cell.FILLED_COLOR_BG
outline = Cell.FILLED_COLOR_BORDER
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
class CellGrid(Canvas):
def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs)
self.cellSize = cellSize
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize))
self.grid.append(line)
#memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
#bind click action
self.bind("<Button-1>", self.handleMouseClick)
#bind moving while clicking
self.bind("<B1-Motion>", self.handleMouseMotion)
#bind release button action - clear the memory of midified cells.
self.bind("<ButtonRelease-1>", lambda event: self.switched.clear())
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def _eventCoords(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
cell._switch()
cell.draw()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
def handleMouseMotion(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
if cell not in self.switched:
cell._switch()
cell.draw()
self.switched.append(cell)
if __name__ == "__main__" :
app = Tk()
grid = CellGrid(app, 50, 50, 10)
grid.pack()
app.mainloop()
I have a problem using the bind option so I can click a button in my keyboard,
and call a function once the key is pressed.
I tried taking other codes that have the similar purpose and I saw that I'm doing pretty much the same, though I still have some problem I must've missed.
I will be very thankful if you help me with this.
Here's my code:
# -*- coding: utf-8 -*-
#Imports
from Tkinter import *
from PIL import ImageTk, Image
import time
import tkMessageBox
#===========================================================================================================================================#
#Tkinter Class
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
#===========================================================================================================================================#
#Game Pieces Classes
class Board:
def __init__(self, rows = 7, columns = 6, picture_height = None, picture_width = None):
self.rows = rows
self.columns = columns
self.picture = ImageTk.PhotoImage(Image.open(r"C:\Users\Ariel\Desktop\Python\4inarow.gif"))
self.picture_height = self.picture.height()
self.picture_width = self.picture.width()
def create_and_pack(self, canvas):
board_item = canvas.create_image(700, 370, image = self.picture)
def __str__(self):
print "Rows:", self.rows, "Columns:", self.columns
class Disk:
def __init__(self, player_number = None):
self.picture = ImageTk.PhotoImage(Image.open(r"C:\Users\Ariel\Desktop\Python\me" + str(player_number) + ".gif"))
self.player_number = player_number
def create_and_pack(self, canvas, x, y):
disk_item = canvas.create_image(x, y, image = self.picture)
def get_x_parameter(self, number):
#X growing by 70~73 per number
x = 330
for i in range(7):
if number == i:
x = x + i * 72
return x
def get_y_parameter(self, number):
#Y lowered by 70~73 per number
y = 635
for i in range(6):
if number == i:
y = y - i * 72
return y
def place(self, canvas, x, y):
#First left down circle parameters
#480, 635
canvas.move(self.picture, x, y)
def __str__(self):
print "The disk's picture string:", self.picture, "The player disk's number:", self.player_number
#===========================================================================================================================================#
#Game Class
class Game:
def __init__(self, board = None, disk1 = None, disk2 = None):
self.disk1 = disk1
self.disk2 = disk2
self.board = board
#===========================================================================================================================================#
#KeyboardClass
class Keyboard:
def __init__(self, key_number = None):
self.key_number = key_number
def press_and_place(self, canvas, number, function):
canvas.focus_set()
canvas.bind("<" + str(number) + ">", function)
#===========================================================================================================================================#
#Main.
myapp = App()
myapp.master.title("4 in a Row")
myapp.master.maxsize(2000, 1200)
#---------------------------------------------------------------------------------------------------#
GameBoard = Board(7, 6)
FirstPlayerDisk = Disk(1)
SecondPlayerDisk = Disk(2)
GameClass = Game(GameBoard, FirstPlayerDisk, SecondPlayerDisk)
#---------------------------------------------------------------------------------------------------#
#Creating Canvas and placing the disks and the board.
board_canvas = Canvas(width = GameBoard.picture_width, height = GameBoard.picture_height)
board_canvas.pack(expand=1, fill=BOTH)
GameBoard.create_and_pack(board_canvas)
FirstPlayerDisk.create_and_pack(board_canvas, 330, 635)
SecondPlayerDisk.create_and_pack(board_canvas, 260, 635)
#---------------------------------------------------------------------------------------------------#
#Creating Keyboard instance and placing the first disk in the last row in a column of choice
number_choice = 3
KeyboardClass = Keyboard(number_choice)
first_player_x = FirstPlayerDisk.get_x_parameter(number_choice)
first_player_y = FirstPlayerDisk.get_y_parameter(number_choice)
KeyboardClass.press_and_place(board_canvas, number_choice, FirstPlayerDisk.place(board_canvas, first_player_x, first_player_y))
#---------------------------------------------------------------------------------------------------#
myapp.mainloop()
Thanks a lot in advance.
I believe your problem lies in this line:
KeyboardClass.press_and_place(board_canvas, number_choice, FirstPlayerDisk.place(board_canvas, first_player_x, first_player_y))
the third argument FirstPlayerDisk.place(board_canvas, first_player_x, first_player_y) is actually a None type as
def place(self, canvas, x, y):
#First left down circle parameters
#480, 635
canvas.move(self.picture, x, y)
returns None
From How to bind a keypress to a button in Tkinter and this site, you need to pass the function, that is, simply FirstPlayerDisk.place (no parenthesis following it).