(PySimpleGUI) How to move an image button to given coords - python

This is almost certainly, very trivial however im dumb so help.
import PySimpleGUI as sg
ChessMenuOption= sg.Button('',image_filename = r'C:\Users\benja\Documents\Python\Chess project\Chess_Selection_Image.png')
sg.Window(title = 'Chess', layout = [[ChessMenuOption]] , size = (800,800)).read()
This places the button at the topleft (0,0), how do I place it somewhere else?

import PySimpleGUI as sg
dummy_element = sg.Text('', size=(100, 100))
ChessMenuOption = sg.Button('', image_filename=r'C:\Users\benja\Documents\Python\Chess project\Chess_Selection_Image.png', button_color=('white', 'white'), pad=(0, 0), size=(800, 800))
layout = [[dummy_element, ChessMenuOption]]
window = sg.Window(title='Chess', layout=layout, size=(900, 900))
window.read()

Button cannot be moved in PySimpleGUI.
If your project is about the chess, you can use the Graph element, then you can move figures in the Graph.
Example Code
import base64
import io
from PIL import Image
import PySimpleGUI as sg
def rotate(data):
file = io.BytesIO(base64.b64decode(data))
im = Image.open(file)
im = im.transpose(Image.ROTATE_180)
with io.BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
width, height = size = (640, 640)
dx, dy = width//8, height//8
ddx, ddy = (dx-56)//2, (dy-56)//2
layout = [
[sg.Graph(size, (0, 0), size, pad=(0, 0), background_color='green', enable_events=True, key='Graph')],
]
window = sg.Window('Chess', layout, margins=(0, 0), finalize=True)
graph = window['Graph']
for i in range(8):
for j in range(8):
graph.draw_rectangle(
((i*dx), (j+1)*dy),
((i+1)*dx, j*dy),
fill_color='white' if (i+j) % 2 else 'black')
figures = {}
for i, image in enumerate(sg.EMOJI_BASE64_HAPPY_LIST[:8]):
figure = graph.draw_image(data=image, location=(i*dx+ddx, dy-ddy))
figures[(i, 0)] = figure
figure = graph.draw_image(data=sg.EMOJI_BASE64_WIZARD, location=(i*dx+ddx, 2*dy-ddy))
figures[(i, 1)] = figure
for i, image in enumerate(sg.EMOJI_BASE64_SAD_LIST[:8]):
figure = graph.draw_image(data=rotate(image), location=(i*dx+ddx, height-ddy))
figures[(i, 7)] = figure
figure = graph.draw_image(data=rotate(sg.EMOJI_BASE64_JEDI), location=(i*dx+ddx, height-dy-ddy))
figures[(i, 6)] = figure
selected, rectangle = False, None
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Graph':
x, y = values[event]
i, j = x//dx, y//dy
if selected:
if (i, j) not in figures:
figure = figures[(i0, j0)]
graph.relocate_figure(figure, i*dx+ddx, (j+1)*dy-ddy)
del figures[(i0, j0)]
i0, j0 = i, j
figures[(i0, j0)] = figure
else:
i0, j0 = i, j
graph.delete_figure(rectangle)
rectangle = graph.draw_rectangle((i0*dx, (j0+1)*dy), ((i0+1)*dx, j0*dy), line_color='yellow', line_width=5)
else:
if (i, j) in figures:
i0, j0 = i, j
rectangle = graph.draw_rectangle((i0*dx, (j0+1)*dy), ((i0+1)*dx, j0*dy), line_color='yellow', line_width=5)
selected = True
else:
if rectangle:
graph.delete_figure(rectangle)
window.close()

Related

Python: threading and tkinter

I'm trying to continuously update matlibplots in tkinter GUI while being able to click on buttons to pause/continue/stop updating the plots. I've tried using threads, but they don't seem to be executing parallelly (e.g. data thread is being executed but the plots don't get updated + clicking on buttons is ignored). Why doesn't it work?
# Import Modules
import tkinter as tk
from threading import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from scipy.fft import fft
import numpy as np
import time
import random
# global variables
state = 1 # 0 starting state; 1 streaming; 2 pause; -1 end and save
x = [0]*12
y = [0]*12
# Thread buttons and plots separately
def threading():
state = 1
t_buttons = Thread(target = buttons)
t_plots = Thread(target = plots)
t_data = Thread(target = data)
t_buttons.start()
t_plots.start()
t_data.start()
def hex_to_dec(x, y):
for i in range(0, 12):
for j in range(0, len(y)):
x[i][j] = int(str(x[i][j]), 16)
y[i][j] = int(str(y[i][j]), 16)
def data():
fig1, axs1 = main_plot()
fig2, axs2 = FFT_plot()
# To be replaced with actual Arduino data
while(state!=-1):
for i in range(0, 12):
x[i] = [j for j in range(101)]
y[i] = [random.randint(0, 10) for j in range(-50, 51)]
for i in range(0, 12):
for j in range(0, len(y)):
x[i][j] = int(str(x[i][j]), 16)
y[i][j] = int(str(y[i][j]), 16)
# create buttons
def stream_clicked():
state = 1
print("clicked")
def pause_clicked():
state = 2
print("state")
def finish_clicked():
state = -1
def buttons():
continue_button = tk.Button(window, width = 30, text = "Stream data" ,
fg = "black", bg = '#98FB98', command = stream_clicked)
continue_button.place(x = window.winfo_screenwidth()*0.2, y = 0)
pause_button = tk.Button(window, width = 30, text = "Pause streaming data" ,
fg = "black", bg = '#FFA000', command = pause_clicked)
pause_button.place(x = window.winfo_screenwidth()*0.4, y = 0)
finish_button = tk.Button(window, width = 30, text = "End session and save",
fg = 'black', bg = '#FF4500', command = finish_clicked())
finish_button.place(x = window.winfo_screenwidth()*0.6, y = 0)
def plots():
fig1, axs1 = main_plot()
fig2, axs2 = FFT_plot()
if state==1:
print("update")
for i in range(0, 12):
axs1[i].plot(x[i], y[i], 'blue')
axs1[i].axes.get_yaxis().set_ticks([0], labels = ["channel " + str(i+1)])
axs1[i].grid(True)
axs1[i].margins(x = 0)
fig1.canvas.draw()
fig1.canvas.flush_events()
for i in range(0, 12):
axs1[i].clear()
for i in range(0, 12):
axs2.plot(x[i], fft(y[i]))
plt.title("FFT of all 12 channels", x = 0.5, y = 1)
fig2.canvas.draw()
fig2.canvas.flush_events()
axs2.clear()
def main_plot():
plt.ion()
fig1, axs1 = plt.subplots(12, figsize = (10, 9), sharex = True)
fig1.subplots_adjust(hspace = 0)
# Add fixed values for axis
canvas = FigureCanvasTkAgg(fig1, master = window)
canvas.draw()
canvas.get_tk_widget().pack()
canvas.get_tk_widget().place(x = 0, y = 35)
return fig1, axs1
def update_main_plot(fig1, axs1):
if state==1:
for i in range(0, 12):
axs1[i].plot(x[i], y[i], 'blue')
axs1[i].axes.get_yaxis().set_ticks([0], labels = ["channel " + str(i+1)])
axs1[i].grid(True)
axs1[i].margins(x = 0)
axs1[0].set_title("Plot recordings", x = 0.5, y = 1)
fig1.canvas.draw()
fig1.canvas.flush_events()
for i in range(0, 12):
axs1[i].clear()
def FFT_plot():
# Plot FFT figure
plt.ion()
fig2, axs2 = plt.subplots(1, figsize = (7, 9))
# Add fixed values for axis
canvas = FigureCanvasTkAgg(fig2, master = window)
canvas.draw()
canvas.get_tk_widget().pack()
canvas.get_tk_widget().place(x = window.winfo_screenwidth()*0.55, y = 35)
return fig2, axs2
def update_FFT_plot(fig2, axs2):
# Update FFT plot
for i in range(0, 12):
axs2.plot(x[i], fft(y[i]))
plt.title("FFT", x = 0.5, y = 1)
fig2.canvas.draw()
fig2.canvas.flush_events()
axs2.clear()
# create root window and set its properties
window = tk.Tk()
window.title("Data Displayer")
window.geometry("%dx%d" % (window.winfo_screenwidth(), window.winfo_screenheight()))
window.configure(background = 'white')
threading()
window.mainloop()
*** Sometimes it just doesn't work without any message and sometimes I also get "RuntimeError: main thread is not in main loop" ***
to be fair all functions in your code are very likely to cause a segmentation fault, and other functions that don't result in a segmentation fault simply don't work, it's hard to explain what's wrong.
define global variables as global if you are going to modify them
update GUI in your main thread by using the window.after method repeatedly.
only reading from your microcontroller should be done in separate thread.
creation of Tkinter objects should be done in the main thread, only updates are allowed in other threads, but it is not thread-safe, so while it may work it can lead to some weird behavior or errors sometimes.
calling matplotlib functions such as ion and flush_events causes errors because these are for matplotlib interactive canvas, not for tkinter canvas.
threading has a very tough learning curve so ask yourself "do i really need threads in here" and "is there any way to not use threads" before you attempt to use them as once you start using threads you are no longer using python's "safe code", despite all the efforts, threads are not safe to use for any task, it's up to you to make them safe, and to be honest threads are not needed here unless you are reading 1 GB/s from your microcontroller.
don't use numbers for states, it's not pythonic, and it confuses the readers, and it has no performance benefit over using Enums.
programs are built incrementally, not copy-paste from multiple working snippets, as it is harder to track where the error comes from when multiple parts of the code weren't verified to be working.
# Import Modules
import tkinter as tk
from threading import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from scipy.fft import fft
import numpy as np
import time
import random
from enum import Enum,auto
UPDATE_INTERVAL_MS = 300
class States(Enum):
STREAM = auto()
PAUSE = auto()
SAVE = auto()
START = auto()
# global variables
state = States.START # check States Enum
x = [[0]]*12
y = [[0]]*12
# Thread buttons and plots separately
def threading():
global state
global window
state = States.STREAM
buttons()
plots()
data()
t_grab_data = Thread(target=grab_data_loop,daemon=True)
t_grab_data.start()
# t_buttons = Thread(target=buttons)
# t_plots = Thread(target=plots)
# t_data = Thread(target=data)
#
# t_buttons.start()
# t_plots.start()
# t_data.start()
def hex_to_dec(x, y):
for i in range(0, 12):
for j in range(0, len(y)):
x[i][j] = int(str(x[i][j]), 16)
y[i][j] = int(str(y[i][j]), 16)
def data():
global fig1,axs1,fig2,axs2
fig1, axs1 = main_plot()
fig2, axs2 = FFT_plot()
# To be replaced with actual Arduino data
window.after(UPDATE_INTERVAL_MS,draw_data_loop)
def grab_data_loop():
while state != States.SAVE:
for i in range(0, 12):
x[i] = [j for j in range(101)]
y[i] = [random.randint(0, 10) for j in range(-50, 51)]
for i in range(0, 12):
for j in range(0, len(y)):
x[i][j] = int(str(x[i][j]), 16)
y[i][j] = int(str(y[i][j]), 16)
time.sleep(0.1) # because we are not reading from a microcontroller
def draw_data_loop():
if state == States.STREAM:
update_main_plot(fig1, axs1)
update_FFT_plot(fig2, axs2)
window.after(UPDATE_INTERVAL_MS,draw_data_loop)
# create buttons
def stream_clicked():
global state
state = States.STREAM
print("clicked")
def pause_clicked():
global state
state = States.PAUSE
print("state")
def finish_clicked():
global state
state = States.SAVE
window.destroy()
def buttons():
continue_button = tk.Button(window, width=30, text="Stream data",
fg="black", bg='#98FB98', command=stream_clicked)
continue_button.place(x=window.winfo_screenwidth() * 0.2, y=0)
pause_button = tk.Button(window, width=30, text="Pause streaming data",
fg="black", bg='#FFA000', command=pause_clicked)
pause_button.place(x=window.winfo_screenwidth() * 0.4, y=0)
finish_button = tk.Button(window, width=30, text="End session and save",
fg='black', bg='#FF4500', command=finish_clicked)
finish_button.place(x=window.winfo_screenwidth() * 0.6, y=0)
def plots():
global state
fig1, axs1 = main_plot()
fig2, axs2 = FFT_plot()
if state == States.STREAM:
print("update")
for i in range(0, 12):
axs1[i].plot(x[i], y[i], 'blue')
axs1[i].axes.get_yaxis().set_ticks([0], labels=["channel " + str(i + 1)])
axs1[i].grid(True)
axs1[i].margins(x=0)
# fig1.canvas.draw()
# fig1.canvas.flush_events()
# for i in range(0, 12):
# axs1[i].clear()
for i in range(0, 12):
axs2.plot(x[i], np.abs(fft(y[i])))
plt.title("FFT of all 12 channels", x=0.5, y=1)
# fig2.canvas.draw()
# fig2.canvas.flush_events()
# axs2.clear()
def main_plot():
# plt.ion()
global canvas1
fig1, axs1 = plt.subplots(12, figsize=(10, 9), sharex=True)
fig1.subplots_adjust(hspace=0)
# Add fixed values for axis
canvas1 = FigureCanvasTkAgg(fig1, master=window)
# canvas.draw()
canvas1.get_tk_widget().pack()
canvas1.get_tk_widget().place(x=0, y=35)
return fig1, axs1
def update_main_plot(fig1, axs1):
if state == States.STREAM:
for i in range(0, 12):
axs1[i].clear()
for i in range(0, 12):
axs1[i].plot(x[i], y[i], 'blue')
axs1[i].axes.get_yaxis().set_ticks([0], labels=["channel " + str(i + 1)])
axs1[i].grid(True)
axs1[i].margins(x=0)
axs1[0].set_title("Plot recordings", x=0.5, y=1)
canvas1.draw()
# fig1.canvas.draw()
# fig1.canvas.flush_events()
def FFT_plot():
# Plot FFT figure
# plt.ion()
global canvas2
fig2, axs2 = plt.subplots(1, figsize=(7, 9))
# Add fixed values for axis
canvas2 = FigureCanvasTkAgg(fig2, master=window)
# canvas.draw()
canvas2.get_tk_widget().pack()
canvas2.get_tk_widget().place(x=window.winfo_screenwidth() * 0.55, y=35)
return fig2, axs2
def update_FFT_plot(fig2, axs2):
# Update FFT plot
if state == States.STREAM:
axs2.clear()
for i in range(0, 12):
axs2.plot(x[i], np.abs(fft(y[i])))
plt.title("FFT", x=0.5, y=1)
canvas2.draw()
# fig2.canvas.draw()
# fig2.canvas.flush_events()
# axs2.clear()
# create root window and set its properties
window = tk.Tk()
window.title("Data Displayer")
window.geometry("%dx%d" % (window.winfo_screenwidth(), window.winfo_screenheight()))
window.configure(background='white')
threading()
window.mainloop()
# put saving logic here

Why won't PySimpleGui.Input adjust the height correctly?

I am trying to create a simple input form for data entry, but I can't get the text input box to change the height, only the width changes when setting the size parameter.
Here is my code:
import PySimpleGUI as sg
def main():
# # create small form for Data Analysis entry
last_printer = sg.Input(key = 'last_printer', size = (10,1))
rejected_carts = sg.Input(key = 'rejected_carts', size = (20, 1))
notes_for_self = sg.Input(key = 'notes_for_self', size = (50,4))
notes_for_ops = sg.Input(key = 'notes_for_ops', size = (50, 4))
notes_for_escalation = sg.Input(key = 'notes_for_escalation', size =(50,4))
layout = [
[sg.Text("Last Printer:"), last_printer],
[sg.Text("Rejected Cartridges:"), rejected_carts],
[sg.Text("Notes for Self:"), notes_for_self],
[sg.Text("Notes for Operators:"), notes_for_ops],
[sg.Text("Notes for Escalation:"), notes_for_escalation],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window("Data Analysis Entry Form", layout) #, finalize=True, keep_on_top=True, grab_anywhere=False, size = (1220, 600))
event, values = window.read()
window.close()
print("event is: " + event)
for each in values.items():
print(each)
if __name__ == '__main__':
main()
last_printer = sg.Multiline(key = 'last_printer', size = (10,1))
rejected_carts = sg.Multiline(key = 'rejected_carts', size = (20, 1))
notes_for_self = sg.Multiline(key = 'notes_for_self', size = (50,4))
notes_for_ops = sg.Multiline(key = 'notes_for_ops', size = (50, 4))

Clear All Images From a Kivy Widget

In this code, self.canvas.clear() does erase a previously drawn board upon resize. However, it doesn't eliminate the images, and I found no method or variable in Image() that would help me do that. Instead, the images duplicate and hang in the background.
Here's the minimal example, all collapsed down from three different programs:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.image import AsyncImage
from kivy.config import Config
from kivy import graphics
def decodeFEN(from_path="FEN_now.txt"):
this_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".split(" ")
piece_placement = this_FEN[0]
for x in range(1, 9):
piece_placement = piece_placement.replace(str(x), "1" * x)
piece_placement = [list(a) for a in piece_placement.split("/")]
this_FEN[0] = piece_placement
print(this_FEN)
return this_FEN
def encodeFEN(): #TODO: Create a program that renders a board state to an FEN.
pass
class GameState():
def __init__(self):
self.fen_state = decodeFEN()
self.board = list(sorted([c for d in
[[(a, (7 - b), self.fen_state[0][b][a], chr(a+97)+str(8-b)) for b in range(0,8)] for a in range(0,8)]
for c in d], key=lambda i: (i[1], i[0])))
# Define the pieces as a dictionary of the FEN keys to image values.
def loadImages():
images = {}
for p in ["b", "k", "n", "q", "p", "r",
"B", "K", "N", "Q", "P", "R"]:
if p == p.lower():
color_tag = "b"
else:
color_tag = "w"
# Populate this dictionary with the relevant image paths.
# images[p] = "Images/{0}{1}.png".format(color_tag, p.upper())
images[p] = "https://kivy.org/logos/kivy-logo-black-64.png"
return images
image_dic = loadImages()
game_state = GameState()
class Chessboard(Widget):
def __init__(self, **kwargs):
super(Chessboard, self).__init__(**kwargs)
self.bind(pos=self.drawBoardandPieces)
self.bind(size=self.drawBoardandPieces)
self.drawBoardandPieces()
def drawBoardandPieces(self, *args):
with self.canvas:
# Reset everything in case of redraws.
self.canvas.clear()
for img in self.ids:
img.piece_img.source = ""
img.piece_img.reload()
# Define the lengths of the edges of the squares.
edge_len = min(self.height, self.width) // 8
Config.set("graphics", "resizable", True)
for column in range(0, 8):
for row in range(0, 8):
if ((row + column) % 2) == 0:
graphics.Color(0, 0, 1)
self.dark_rect = graphics.Rectangle(pos=(column*edge_len, row*edge_len), size=(edge_len, edge_len))
else:
graphics.Color(1, 1, 1)
self.light_rect = graphics.Rectangle(pos=(column*edge_len, row*edge_len), size=(edge_len, edge_len))
piece_in_pos = [a[2] for a in game_state.board if (a[0] == column) and (a[1] == row)][0]
if piece_in_pos != "1":
# self.piece_img = Image(source=image_dic[piece_in_pos])
self.piece_img = AsyncImage(source=image_dic[piece_in_pos])
self.piece_img.allow_stretch = True
self.piece_img.keep_ratio = True
self.piece_img.opacity = 1
self.piece_img.pos = (column*edge_len, row*edge_len)
self.piece_img.size = (edge_len, edge_len)
self.add_widget(self.piece_img)
class SCApp(App):
def build(self):
app_layout = BoxLayout(orientation="horizontal")
app_layout.add_widget(widget=Chessboard())
return app_layout
SCApp().run()
The canvas.clear() method only clears canvas instructions, things like Color, Rectangle, etc. It does not affect the list of child widgets (those added using add_widget()). If you are completely redrawing your chess board, then you should probably clear the child widgets as well as clearing the canvas. Try adding:
self.clear_widgets()
along with your canvas.clear().
Thanks to notes from #inclement and #john-anderson, I solved the issue. It turns out that (drawings of[?]) the images appear behind the drawn board, as well, but can't be deleted with self.clear_widgets():
def drawBoardandPieces(self, *args):
with self.canvas:
# Reset everything in case of redraws.
self.canvas.clear()
# Define the lengths of the edges of the squares.
edge_len = min(self.height, self.width) // 8
Config.set("graphics", "resizable", True)
for column in range(0, 8):
for row in range(0, 8):
if ((row + column) % 2) == 0:
graphics.Color(0, 0, 1)
self.dark_rect = graphics.Rectangle(pos=(column*edge_len, row*edge_len), size=(edge_len, edge_len))
else:
graphics.Color(1, 1, 1)
self.light_rect = graphics.Rectangle(pos=(column*edge_len, row*edge_len), size=(edge_len, edge_len))
self.clear_widgets()
for column in range(0, 8):
for row in range(0, 8):
piece_in_pos = [a[2] for a in game_state.board if (a[0] == column) and (a[1] == row)][0]
if piece_in_pos != "1":
self.piece_img = Image(source=image_dic[piece_in_pos])
self.piece_img.allow_stretch = True
self.piece_img.keep_ratio = True
self.piece_img.opacity = 1
self.piece_img.pos = (column*edge_len, row*edge_len)
self.piece_img.size = (edge_len, edge_len)
self.add_widget(self.piece_img)

PyQtgraph - Draw ROI by mouse click & drag

I would like to draw ROI's by click and drag events in the PlotWidget. The issue is that several click interactions are already reserved for the PlotWidget, second it is hard to tell the right position of the mouse in the PlotWidget - especially when the image scale has been changed or if the window scale has been changed.
import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets
from PyQt5 import Qt
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import vtk, sys
import numpy as np
from PIL import Image
class GUI:
def __init__(self):
self.init_gui()
def proxyWidget(self, item, width=None, height=None):
proxy = QtGui.QGraphicsProxyWidget()
if(height != None):
height = item.sizeHint().height() if height==None else height
item.setMaximumHeight(height)
if(width!=None):
width = item.sizeHint().width() if width==None else width
item.setMaximumWidth(width)
proxy.setWidget(item)
return proxy
def init_gui(self, win_height=800, win_width=1800):
pg.setConfigOptions(imageAxisOrder='row-major')
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.w = pg.GraphicsWindow(size=(win_width,win_height), border=True)
self.img = pg.ImageItem()
self.list_imgs = QtGui.QListWidget()
self.btn_Del_Mark = QtGui.QPushButton('Del Mark')
self.btn_MarkPed = QtGui.QPushButton('Mark ped')
self.lbl_list1 = QtGui.QLabel("List Images")
self.lbl_list2 = QtGui.QLabel("List Markings")
self.list_imgs = QtGui.QListWidget()
self.list_marks = QtGui.QListWidget()
self.layout = QtGui.QGridLayout()
self.w.setLayout(self.layout)
#self.w_3d = pg.GraphicsWindow()
self.vtkWidget = QVTKRenderWindowInteractor()
#self.w_3d.addItem(self.proxyWidget(self.vtkWidget))
self.vtkWidget.Initialize()
self.vtkWidget.Start()
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
# Create source
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.iren.Initialize()
self.iren.Start()
path = "/home/brain/uni/frustum-pointnets/dataset/KITTI/object/testing/image_2/000000.png"
imgdata = Image.open(path)
self.imgArr = np.array(imgdata)
#ToDo: undistort Image if neccessary
self.img.setImage(self.imgArr)
#self.vbLayout = self.w.addLayout(row=0, col=3, rowspan=10, colspan=20)
imageGraph = pg.PlotWidget(name='Signalgraph')
self.vb = imageGraph.plotItem.vb
self.lbl_list1.setAlignment(QtCore.Qt.AlignCenter)
self.lbl_list2.setAlignment(QtCore.Qt.AlignCenter)
self.vb.setAspectLocked()
self.vb.addItem(self.img)
self.vb.invertY(True)
self.vb.setMaximumSize(int(7/10.*win_width), int(9/20.*win_height))
self.layout.addWidget(imageGraph, 1 , 3, 10, 20)
self.layout.addWidget(self.vtkWidget , 11, 3, 10, 20)
self.layout.addWidget(self.lbl_list1 , 0, 1, 1, 1)
self.layout.addWidget(self.lbl_list2 , 0, 2, 1, 1)
self.layout.addWidget(self.list_imgs , 1, 1, 20,1)
self.layout.addWidget(self.list_marks, 1, 2, 20,1)
sizeHint = lambda: pg.QtCore.QSize(int(1./10.*win_width), int(0.9/20.*win_height))
self.lbl_list1.sizeHint = lambda: pg.QtCore.QSize(int(1./10.*win_width), int(0.9/20.*win_height))
self.lbl_list2.sizeHint = lambda: pg.QtCore.QSize(int(1./10.*win_width), int(0.9/20.*win_height))
self.list_imgs.sizeHint = lambda: pg.QtCore.QSize(int(1./10.*win_width), int(18/20.*win_height))
self.list_marks.sizeHint = lambda: pg.QtCore.QSize(int(1./10.*win_width), int(18/20.*win_height))
self.list_imgs.setMaximumWidth(int(1./10.*win_width))
self.list_marks.setMaximumWidth(int(1./10.*win_width))
self.vtkWidget.show()
if __name__ == "__main__":
app = QtGui.QApplication([])
guiobj = GUI()
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
I would like to start drawing the ROI by mouse click and stop drawing by mouse release... every hint would be helpful. Please consider that the content of the PlotWidget is drag-able and that it might need to be frozen while drawing a ROI.
EDIT:
I have tried to overwrite temporary the click events with the following lines, but somehow clickevents seem to be triggered somewhere else, since my functions do not get called...
def on_btn_MarkPed(self):
#self.vb.setMouseEnabled(x=False, y=False)
self.creatRoiByMouse("Pedestrian")
def on_btn_MarkCycl(self):
self.creatRoiByMouse("Cyclist")
def on_btn_MarkVehicle(self):
self.creatRoiByMouse("Vehicle")
def creatRoiByMouse(self, class2Mark):
self.img.mousePressEvent = self.ImgMousePressEvent
self.img.mouseReleaseEvent = self.ImgMouseReleaseEvent
def ImgMousePressEvent(self, event):
print(event)
pass
#
#
def ImgMouseReleaseEvent(self, event):
print(event)
pass
I know this post is old, but in case someone else finds it someday I thought I'd post my solution. It's inelegant, but it works for me. In my application, I have a button labeled "draw" that calls a function that overwrites the mouse drag event temporarily to simply draw a box instead. The overwritten mouse drag event restores the native mouse drag event with the finish signal. This code also overwrites the escape key to cancel the draw if pressed after pushing my draw button but before drawing. In my code, imageArrayItem is an existing imageItem in a plot widget, and Dialog is a QDialog containing the plot widget.
def clickDraw(self):
app.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
imageArrayItem.getViewBox().setMouseMode(ViewBox.RectMode)
imageArrayItem.getViewBox().rbScaleBox.setPen(fn.mkPen((255, 255, 255), width = 1))
imageArrayItem.getViewBox().rbScaleBox.setBrush(fn.mkBrush(255, 255, 255, 100))
def mouseDragEvent(ev, axis = None): # This is a modified version of the original mouseDragEvent function in pyqtgraph.ViewBox
ev.accept() # accept all buttons
dif = (ev.pos() - ev.lastPos()) * -1
mouseEnabled = np.array(imageArrayItem.getViewBox().state['mouseEnabled'], dtype = np.float)
mask = mouseEnabled.copy()
if ev.button() & QtCore.Qt.LeftButton:
if imageArrayItem.getViewBox().state['mouseMode'] == ViewBox.RectMode:
if ev.isFinish():
QtCore.QTimer.singleShot(0, self.restoreCursor)
imageArrayItem.getViewBox().rbScaleBox.hide()
ax = QtCore.QRectF(Point(ev.buttonDownPos(ev.button())), Point(ev.pos()))
ax = imageArrayItem.getViewBox().childGroup.mapRectFromParent(ax)
imageArrayItem.getViewBox().mouseDragEvent = temp # reset to original mouseDragEvent
imageArrayItem.getViewBox().setMouseMode(ViewBox.PanMode)
else:
imageArrayItem.getViewBox().updateScaleBox(ev.buttonDownPos(), ev.pos()) # update shape of scale box
elif ev.button() & QtCore.Qt.MidButton: # allow for panning with middle mouse button
tr = dif*mask
tr = imageArrayItem.getViewBox().mapToView(tr) - imageArrayItem.getViewBox().mapToView(Point(0,0))
x = tr.x() if mask[0] == 1 else None
y = tr.y() if mask[1] == 1 else None
imageArrayItem.getViewBox()._resetTarget()
if x is not None or y is not None:
imageArrayItem.getViewBox().translateBy(x=x, y=y)
imageArrayItem.getViewBox().sigRangeChangedManually.emit(imageArrayItem.getViewBox().state['mouseEnabled'])
def keyPressE_mouseDrag(event): # Override "esc" key to cancel draw
if event.key() == QtCore.Qt.Key_Escape:
QtCore.QTimer.singleShot(0, self.restoreCursor)
imageArrayItem.getViewBox().rbScaleBox.hide()
imageArrayItem.getViewBox().mouseDragEvent = temp # reset to original mouseDragEvent
imageArrayItem.getViewBox().setMouseMode(ViewBox.PanMode)
else:
QtWidgets.QDialog.keyPressEvent(Dialog, event)
Dialog.keyPressEvent = keyPressE_mouseDrag
temp = imageArrayItem.getViewBox().mouseDragEvent # save original mouseDragEvent for later
imageArrayItem.getViewBox().mouseDragEvent = mouseDragEvent # set to modified mouseDragEvent

Why won't my .gif files show up when I call them using PhotoImage?

from tkinter import *
from random import *
from functools import partial
class Game:
def __init__(self):
self.root = Tk()
self.frame = Frame(width = 574, height = 574)
self.frame.grid(columnspan = 30, rowspan = 30)
self.minex = []
self.miney = []
self.clickx = 0
self.clicky = 0
blank = PhotoImage(file = 'C:\\Users\\PC\\Desktop\\Python Programs\\Minesweeper\\blank.gif')
for i in range(0,30):
for j in range(0,30):
button = Button(width = 15, height = 15, padx = 2, pady = 2, image = blank, command = partial(self.click, j, i))
button.grid(row = i, column = j)
self.mine_place()
self.root.mainloop()
def mine_place(self):
for i in range(0,15):
self.minex.append(randint(1,30))
self.miney.append(randint(1,30))
def click(self, j, i):
miss = PhotoImage(file = 'C:\\Users\\PC\\Desktop\\Python Programs\\Minesweeper\\miss.gif')
hit = PhotoImage(file = 'C:\\Users\\PC\\Desktop\\Python Programs\\Minesweeper\\hit.gif')
for k in range(0, len(self.minex)):
if j + 1 == self.minex[k] and i + 1 == self.miney[k]:
button = Button(image = hit)
button.grid(row = i, column = j)
else:
button = Button(image = miss)
button.grid(row = i, column = j)
app = Game()
In self.click, when I wish to create a button with this image I am given a blank image. If I create a button in init, the image comes out just fine. What is wrong?..............................................................
It looks like you're images are getting garbage collected you need to save a reference to the images after using PhotoImage.
ie - you create the image blank so save a reference as self.blank= blank and use image = self.hit

Categories

Resources