In Gtk, how to make a window smaller when creating - widget

I am trying to display both an image and a box with an Entry widget. I can do that, but the window is so large that the widget at the bottom is mostly out of view. I have tried several calls to set the window's size or unmaximize it, but they seem to have no effect. I determined that the problem only occurs when the image is large, but still wonder how to display a large image in a resizable window or, for that matter, to make any changes to the window's geometry from code. All the function call I tried seem to have no effect.
Here is my code:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from urllib.request import urlopen
class Display(object):
def __init__(self):
self.window = Gtk.Window()
self.window.connect('destroy', self.destroy)
self.window.set_border_width(10)
# a box underneath would be added every time you do
# vbox.pack_start(new_widget)
vbox = Gtk.VBox()
self.image = Gtk.Image()
response = urlopen('http://1.bp.blogspot.com/-e-rzcjuCpk8/T3H-mSry7PI/AAAAAAAAOrc/Z3XrqSQNrSA/s1600/rubberDuck.jpg').read()
pbuf = GdkPixbuf.PixbufLoader()
pbuf.write(response)
pbuf.close()
self.image.set_from_pixbuf(pbuf.get_pixbuf())
self.window.add(vbox)
vbox.pack_start(self.image, False, False, 0)
self.entry = Gtk.Entry()
vbox.pack_start(self.entry, True,True, 0)
self.image.show()
self.window.show_all()
def main(self):
Gtk.main()
def destroy(self, widget, data=None):
Gtk.main_quit()
a=Display()
a.main()

Most of the posted information seems to pertain to Gtk2 rather than Gtk3, but there is a solution: to use a pix buf loader and set the size:
from gi.repository import Gtk, Gdk, GdkPixbuf
#more stuff
path = config['DEFAULT']['datasets']+'working.png'
with open(path,'rb') as f:
pixels = f.read()
loader = GdkPixbuf.PixbufLoader()
loader.set_size(400,400)
loader.write(pixels)
pb = GdkPixbuf.Pixbuf.new_from_file(path)
self.main_image.set_from_pixbuf(loader.get_pixbuf())
loader.close()

Related

Shadows not showing up in Pybullet getCameraImage

I am trying to produce shadows in a captured image in the Pybullet simulator. The shadows show up in the debugGUI mode but are not visible when calling p.getCameraImage even with shadow=True.
Something else I noticed is when shadow=True in p.getCameraImage there is a large shadow as if there is a tall object blocking the light rather than a small shadow corresponding to the object.
The issue persists whether connected to direct (p.connect(p.DIRECT)) or to GUI (p.connect(p.GUI)) as suggested here: https://github.com/bulletphysics/bullet3/issues/2610 and I have attached a minimum code sample below needed to reproduce the issue.
import pybullet as p
import pybullet_data
from PIL import Image
import math
physicsClient = p.connect(p.DIRECT)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
p.configureDebugVisualizer(p.COV_ENABLE_SHADOWS, 1) # Shadows on/off
planeId = p.loadURDF("plane.urdf")
p.loadURDF("sphere_small.urdf",[0,0,0.2])
projectionMatrix = p.computeProjectionMatrixFOV(
fov=90.0,
aspect=16. / 9.,
nearVal=0.1,
farVal=30)
for i in range(360):
viewMatrix = p.computeViewMatrix(
cameraEyePosition=[2*math.cos(math.radians(i)), 2*math.sin(math.radians(i)),2],
cameraTargetPosition=[0.0, 0, 0],
cameraUpVector=[0, 0, 1])
width, height, rgbImg, depthImg, segImg = p.getCameraImage(
width=int(1280),
height=int(720),
viewMatrix=viewMatrix,
projectionMatrix=projectionMatrix,
shadow=True
)
im = Image.fromarray(rgbImg)
im.save('imgs/test/' + str(i) + ".png")

Python - couldn't open image

IMAGE WITH ERROR IN MY PYTHON GAME:
I want to run my application, but see this error, how to fix it?
Image with a correctly format is not a possible to open, this is a simple game make in a Python
import random
from livewires import games
games.init(screen_width=640,screen_height=480,fps=50)
class Ship(games.Sprite):
def update(self):
if games.keyboard.is_pressed(games.K_RIGHT):
self.angle+=1
if games.keyboard.is_pressed(games.K_LEFT):
self.angle-=1
if games.keyboard.is_pressed(games.K_1):
self.angle=0
if games.keyboard.is_pressed(games.K_2):
self.angle=90
if games.keyboard.is_pressed(games.K_3):
self.angle=180
if games.keyboard.is_pressed(games.K_4):
self.angle=270
class Asteroid(games.Sprite):
SMALL = 1
MEDIUM = 2
LARGE = 3
images={SMALL : games.load_image("asteroida_s.bmp"),
MEDIUM : games.load_image('asteroida_m.bmp'),
LARGE : games.load_image('asteroida_l.bmp') }
SPEED=2
def main():
nebula_image=games.load_image("mglawica.jpg")
games.screen.background=nebula_image
for i in range(8):
x=random.randrange(games.screen.width)
y=random.randrange(games.screen.height)
size-random.choice([Asteroid.SMALL,Asteroid.MEDIUM,Asteroid.LARGE,])
new_asteroid=Asteroid(x=x,y=y,size=size)
game.screen.add(new_asteroid)
games.screen.mainloop()
def main():
nebula_image=games.load_image("mglawica.jpg",transparent=false)
games.screen.background=nebula_image
ship_image=games.load_image("statek.bmp")
the_ship=Ship(image=ship_image,
x=games.scren.width/2,
y=games.scren.height/2)
games.scren.add(the_ship)
games.screen.mainloop()
def play(self):
nebula_image=games.load_image("mglawica.jpg")
games.screen.background=nebula_image
self.advance()
games.screen.mainloop()
def advanced(self):
self.level+=1
BUFFER=150
for i in range(self.level):
x_min=random.randrage(BUFFER)
y_min=BUFFER-x_min
x=self.ship.x+x_distance
y=self.ship.y+y_distance
x%=games.screen.width
y%=games.screen.height
new_asteroid=Asteroid(game=self,x=x,y=y,size=Asteroid.LARGE)
game.screen.add(new_asteroid)
def end():
end_message=games.Message(value="Koniec gry",
size=90,
color=color.red,
x=games.screen.width/2,
y=games.screen.height/2,
lifetime=10*games.scren.fps,after_death=games.screen.quit,
is_sollideable=False)
games.scren.add(end_message)
def die(self):
if self.size !=Asteroid.SMALL:
for i in range(Asteroid.SMALL):
new_asteroid=Asteroid(x=self.x,
y=self.y,
size=self.size-1)
game.screen.add(new_asteroid)
self.destroy()
While your code is very messy (no indentation), I think I have a way to fix it. First, I question your need to import livewire, and also wonder why you marked this question as pygame. You never imported pygame. So my solution will be written in pygame.
First, you have to load the images (and if you want assign it to a variable):
image = pygame.image.load(filename)
Then, you have to convert the Surface, or basically just the image.
image = pygame.image.load(filename).convert()

how to get mouse position in

I am a new pyqtgraph users,try to "Embedding widgets inside PyQt applications"following the instructions in http://www.pyqtgraph.org/documentation/how_to_use.html. in my example I promote Graphics view to PlotWidget, then save as "test2.ui", also follow the "crosshair/mouse interaction" example,my code:
import sys
import numpy
from PyQt5 import QtCore, QtGui,uic,QtWidgets
from PyQt5.QtWidgets import *
import pyqtgraph as pg
import os
hw,QtBaseClass=uic.loadUiType("test.ui")
def gaussian(A, B, x):
return A * numpy.exp(-(x / (2. * B)) ** 2.)
class MyApp(QtWidgets.QMainWindow, hw):
def __init__(self):
super().__init__()
self.setupUi(self)
winSize=self.size()
self.view.resize(winSize.width(),winSize.height())
x = numpy.linspace(-5., 5., 10000)
y =gaussian(5.,0.2, x)
self.p=self.view.plot(x,y)
proxy = pg.SignalProxy(self.view.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
self.view.enableAutoRange("xy", True)
def mouseMoved(evt):
print("mouseTest")
mousePoint = self.p.vb.mapSceneToView(evt[0])
label.setText(
"<span style='font-size: 14pt; color: white'> x = %0.2f, <span style='color: white'> y = %0.2f</span>" % (
mousePoint.x(), mousePoint.y()))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
it seems not get the mouse move event;
after change
proxy = pg.SignalProxy(self.view.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
to
self.view.scene().sigMouseMoved.connect(self.mouseMoved),
output"MouseTest",but program imediatly crash.
can any one give me some help
Two things:
Re: Crashing
It seems as if you haven't placed a label in the GUI to modify, perhaps your code is seeing this and kicks it back to you. If you're using qtDesigner, it is likely defined as self.label, and in my GUI, I was required to use self.label to reference it.
Re: mouseMoved function
I was just struggling with a similar issue of it not working. I was able to get mine to work by changing the evt[0] to simply evt, something I think they moved to from pyqt4 to pyqt5.
Here's an example of what I was able to get to work:
..........setup code above... IN THE setupUi function:
..........setup code above...
Plotted = self.plot
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
Plotted.addItem(vLine, ignoreBounds=True)
Plotted.addItem(hLine, ignoreBounds=True)
Plotted.setMouseTracking(True)
Plotted.scene().sigMouseMoved.connect(self.mouseMoved)
def mouseMoved(self,evt):
pos = evt
if self.plot.sceneBoundingRect().contains(pos):
mousePoint = self.plot.plotItem.vb.mapSceneToView(pos)
self.label.setText("<span style='font-size: 15pt'>X=%0.1f, <span style='color: black'>Y=%0.1f</span>" % (mousePoint.x(),mousePoint.y()))
self.plot.plotItem.vLine.setPos(mousePoint.x())
self.plot.plotItem.hLine.setPos(mousePoint.y()
...the if__name__ =="__main__": function .....
In my case, I did not pass the proxy statement, and instead just went for the sigMouseMoved since it already passes the information the proxy would. I think this was in the example in pyqt5 (and commented out) because it was the change. However, the comment didn't specifically state this.

IPython notebook widgets using interactive

I'm having trouble creating widgets in a Jupyter notebook that update when other widget values are changed. This is the code I've been playing around with:
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
from IPython.display import display
def func(arg1,arg2):
print arg1
print arg2
choice = widgets.ToggleButtons(description='Choice:',options=['A','B'])
display(choice)
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a = interactive(func,
arg1=metric,
arg2=text,
__manual=True)
def update(*args):
if choice.value == 'A':
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a.children = (metric,text)
else:
metric = widgets.Dropdown(options=['monday','tuesday','wednesday'],description='Days:')
text2 = widgets.Textarea(description='Text2:')
a.children = (metric,text2)
choice.observe(update,'value')
display(a)
The resulting widgets metric and text do change based whether A or B is selected, but the problem is that the "Run func" button goes away as soon as I change to B. I've tried adding the __manual attribute immediately before display(a), adding it within update, and several other places. How do I change the children of the widget box without overwriting the fact that I want to manually run the function?

How do I rerender HTML PyQt4

I have managed to use suggested code in order to render HTML from a webpage and then parse, find and use the text as wanted. I'm using PyQt4. However, the webpage I am interested in is updated frequently and I want to rerender the page and check the updated HTML for new info.
I thus have a loop in my pythonscript so that I sort of start all over again. However, this makes the program crash. I have searched the net and found out that this is to be expected, but I have not found any suggestion on how to do it correctly. It must be simple, I guess?
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
class Render (QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
r = Render(url)
html = r.frame.toHtml()
S,o when I hit r=Render(url) the second time, it crashes. S,o I am looking for something like r = Rerender(url).
As you might guess, I am not much of a programmer, and I usually get by by stealing code I barely understand. But this is the first time I can't find an answer, so I thought I should ask a question myself.
I hope my question is clear enough and that someone has the answer.
Simple demo (adapt to taste):
import sys, signal
from PyQt4 import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
def __init__(self, url):
super(WebPage, self).__init__()
self.url = url
self.mainFrame().loadFinished.connect(self.handleLoadFinished)
self.refresh()
def refresh(self):
self.mainFrame().load(QtCore.QUrl(self.url))
def handleLoadFinished(self):
print('Loaded:', self.mainFrame().url().toString())
# do stuff with html ...
print('Reloading in 3 seconds...\n')
QtCore.QTimer.singleShot(2000, self.refresh)
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtGui.QApplication(sys.argv)
webpage = WebPage('http://en.wikipedia.org/')
print('Press Ctrl+C to quit\n')
sys.exit(app.exec_())