how to stop Vpython from being unresponsive - button

Im fairly new to Vpython and Ive created a simulation for circular motion. I tried adding controls to the simulation and so far I am able to pause the simulation but when i do pause it, the entire control panel becomes unresponsive and i am unable to resume the simulation. Does anyone know why this may be or how i can fix this?
def playorpause(self, goorno):
self.pause = goorno
def actual_sim(self):
c = controls() # Create controls window
# Create a button in the controls window:
# b = Button(pos=(-50, 0), width=60, height=60, text="Pause", action=lambda: self.change)
# play = Button(pos=(50, 0), width=60, height=60, text="Play", command = self.play_sim)
b = button(pos=(-50, 0), width=60, height=60, text='Pause', action=lambda: self.playorpause(True))
play = button(pos=(50,0), width = 60, height = 60, text = "play", action =lambda: self.playorpause(False))
def loop():
#### simulation code
if self.pause == False:
loop()

I'm not sure, because I don't really understand your code, but a repetitive loop needs to include a rate statement. Otherwise the program will lock up, unable to output anything.

Related

awesome-wm wibox not reacting to mouse signals when input_passthrough is true

Basically, what I'm trying to do is have a sidebar, and have it show up when I move my mouse to the left edge of the screen, and have the sidebar disappear when I move my mouse TOO MUCH away from the sidebar.
So I made three widgets:
one that's one pixel wide on the side and that, when it detects the mouse entering, shows the sidebar
the actual sidebar
and a widget that's wider than the sidebar, is fully transparent, has input_passthrough set to true, and its only purpose is to look for the "mouse::leave" signal and when the mouse leaves, it would make itself and the sidebar disappear.
I got most of it to work but there's this particular error that I can't solve:
The wibox function takes as argument a table of fields. If you look through the code I provided, you can notice that the input_passthrough field of the sidebar_visible_limit hasn't been put in the body of the table, but supplied afterwards, right after the creation of the widget.
The problem with this is that it simply doesn't close the sidebar or itself when the mouse leaves. It doesn't detect the mouse leaving.
BUT if put the input_passthrough = true in the table provided to the wibox function, like this:
bar.sidebar_visible_limit = wibox({
x = 0,
y = 0,
ontop = false,
visible = false,
width = bar.sidebar.width + dpi(100),
height = bar.sidebar.height,
bg = '#000000',
opacity = 0.3, -- when it's all done this will be '0'
input_passthrough = true
})
then everything works fine, EXCEPT that now it doesn't allow input to pass through.
I would greatly appreciate an explanation as to why this happens.
This is the code:
awful = require("awful")
local wibox = require("wibox")
local naughty = require("naughty")
local gears = require("gears")
local beautiful = require("beautiful")
xresources = require("beautiful.xresources")
dpi = xresources.apply_dpi
bar = {}
-- make the sidebar
bar.sidebar = wibox({
x = 0,
y = 0,
ontop = false,
visible = false,
width = beautiful.sidebar_width or dpi(450),
bg = beautiful.sidebar_bg or "#2f2e3a",
type = "dock",
height = beautiful.sidebar_height or awful.screen.focused().geometry.height,
})
-- Hide sidebar when mouse leaves too much from the sidebar
-- It's incorporated along in the same table with the sidebar so the users
-- can implement these however they want, e.g. in the 'keys.lua' module
bar.sidebar_visible_limit = wibox({
x = 0,
y = 0,
ontop = false,
visible = false,
width = bar.sidebar.width + dpi(100),
height = bar.sidebar.height,
bg = '#000000',
opacity = 0.3, --when it's all done this will be '0'
})
bar.sidebar_visible_limit.input_passthrough = true
-- Show sidebar when mouse touches edge
local sidebar_displayer = wibox({
x = 0,
y = 0,
height = bar.sidebar.height,
ontop = true,
width = 1,
visible = true,
opacity = 0,
input_passthrough = true
})
function toggle_bar()
-- they have to be in this order, so the sidebar will show first,
-- and then the wibox that will close the sidebar when the mouse leaves
-- second. If you do it the other way around, then if you go with the
-- mouse on the sidebar-closing wibox , then if you try to go back
-- to the sidebar, it will close it because it's 'left' the widget.
-- That's why you have the sidebar-closing wibox on top and allow
-- input_passthrough for the sidebar-closing wibox
bar.sidebar.visible = not bar.sidebar.visible
bar.sidebar.ontop = not bar.sidebar.ontop
bar.sidebar_visible_limit.visible = not bar.sidebar_visible_limit.visible
bar.sidebar_visible_limit.ontop = not bar.sidebar_visible_limit.ontop
end
bar.sidebar_visible_limit:connect_signal( "mouse::leave", toggle_bar )
sidebar_displayer:connect_signal( "mouse::enter", toggle_bar )
You could use a mousegrabber to detect the position of the mouse:
bar.sidebar.ontop = true
function show_sidebar()
bar.sidebar.visible = true
mousegrabber.run(function(mouse)
if mouse.x > bar.sidebar.width + dpi(100) then
hide_sidebar()
return false
end
return true
end)
end
function hide_sidebar()
bar.sidebar.visible = false
end
Alternatively, you could use the same mousegrabber to decide when to show the sidebar too:
bar.sidebar.ontop = true
function show_sidebar()
bar.sidebar.visible = true
end
function hide_sidebar()
bar.sidebar.visible = false
end
mousegrabber.run(function(mouse)
if bar.sidebar.visible then
if mouse.x > bar.sidebar.width + dpi(100) then
hide_sidebar()
end
else
if mouse.x == 1 then
show_sidebar()
end
end
return true
end)
I think this is nicer than creating an invisible wibox over the whole screen.
I would greatly appreciate an explanation as to why this happens.
I think the X11 server reports mouse enter/exit relative to the input region of a window. With input_passthrough, the input region becomes empty. This means that the X11 server will now report the mouse pointer to be inside the window beneath your wibox and not inside of the wibox itself.
BUT if put the input_passthrough = true in the table provided to the wibox function then everything works fine, EXCEPT that now it doesn't allow input to pass through.
In other words: In this case the input_passthrough property is not set. Seems like you found one of the properties that cannot be set this way. ;-)
Since I guess you also want some ideas on how you can do what you are trying to do: If you are running a compositing manager (xcompmgr, compton, ...), you could make a wibox with a completely transparent background. That way, the X11 server will "think" that the window is there and report input events relative to it, but the wibox will not actually be visible on screen.
(And if you don't have a compositing manager: Make a widget that displays your wallpaper with the proper offset. A hint on how to do this can be found by reading /usr/share/awesome/lib/wibox/drawable.lua, the part that begins with if not capi.awesome.composite_manager_running then and that does something with the wallpaper. Feel free to ask for more details if necessary.)

Closing window when object reaches bottom

I want to display a message, and close the window when the user clicks. This should happen when the circle reaches the bottom of the window. I'm not sure how to go about this, everything works fine until the circle passes the bottom of the window, the closing message doesn't pop up and the window doesnt close on click. I'm using the graphics.py graphics library from Zelle for Python. I'm a beginner in Python so my knowledge is very limited right now. My code is as follows:
from graphics import *
def q2a():
win = GraphWin("window",400,400)
win.setCoords(0,0,400,400)
win.setBackground("light grey")
#drawing circle
circle = Circle(Point(200,100),30)
circle.setFill("red")
circle.draw(win)
#text
message = Text(Point(200,200),"Click Anywhere to Begin")
message.draw(win)
#clicking
while True:
click = win.checkMouse()
if click:
message.undraw()
while circle.getCenter().getY() < 170:
dy=1
dx = 0
dy *=-.01
circle.move(dx,dy)
if circle.getCenter()== 0:
circle.undraw()
gameover = Text(Point(200,200),"Game Over - Click to Close")
gameover.draw(win)
win.checkMouse()
win.close()
q2a()
I believe the problem is simpler than you're making it. One problem is that this is an infinite loop:
while circle.getCenter().getY() < 170:
dy=1
dx = 0
dy *=-.01
circle.move(dx,dy)
As circle's Y center starts at 100 and decreases, so it's always less than 170 so this loop never finishes and any code beyond this point is never executed. Let's use the circle's radius, 30, instead so the circle stops when it sits on the bottom of the window.
Another issue is that I believe you're using checkMouse() when you really want getMouse(). Read the documentation about the difference between these two commands.
Here's my rework of your code (with some style tweaks.) I changed the -0.01 increment to -0.1 as I've no patience!
from graphics import *
RADIUS = 30
HEIGHT, WIDTH = 400, 400
CENTER = Point(HEIGHT / 2, WIDTH / 2)
def q2a():
win = GraphWin("window", HEIGHT, WIDTH)
win.setCoords(0, 0, HEIGHT, WIDTH)
win.setBackground("light grey")
# drawing circle
circle = Circle(Point(WIDTH / 2, 100), RADIUS)
circle.setFill("red")
circle.draw(win)
# text
message = Text(CENTER, "Click Anywhere to Begin")
message.draw(win)
# moving
win.getMouse()
message.undraw()
while circle.getCenter().getY() > RADIUS:
circle.move(0, -0.1)
# end game
circle.undraw()
gameover = Text(CENTER, "Game Over - Click to Close")
gameover.draw(win)
win.getMouse()
win.close()
q2a()

How do I mouse-drag-zoom a QGraphicsView anchored to the mouse drag starting position?

I'm trying to click and drag to zoom in and out of a QGraphicsView like you see in graphics applications like Maya and Nuke. There is a lot of information about using the mouse wheel but I haven't found anything related to dragging to zoom.
Is there an easy way to do this or do I need to roll my own implementation of the "anchor" effect?
The following will work but the view follows the mouse around as I drag the zoom rather than appearing to zoom in and out of a fixed point in space (the point where the mouse was clicked to start the drag-zoom.
(This is a bunch of copy and paste from my more complex source code. It is intended to be illustrative though it should run)
def mousePressEvent(self, event):
self.press_mouse_pos = event.pos()
transform = self.transform()
self.press_translate = [transform.m31(), transform.m32()]
self.press_scale = transform.m11()
if event.button() == QtCore.Qt.RightButton and \
event.modifiers() == QtCore.Qt.AltModifier:
self.scaling = True
event.accept()
else:
super(GraphView, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.scaling:
delta_pos = event.pos() - self.press_mouse_pos
amount = delta_pos.x() + delta_pos.y()
speed = 0.001
scl = self.press_scale - (amount * speed)
scl = min(1.0, max(scl, 0.1)) # Clamp so we don't go to far in or out
transform = QtGui.QTransform(
scl, 0, 0,
0, scl, 0,
self.press_translate[0], self.press_translate[1], 1
)
# If interactive is True then some double calculations are triggered
prev_interactive_state = self.isInteractive()
prev_anchor_mode = self.transformationAnchor()
self.setInteractive(False)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.setTransform(transform)
self.setInteractive(prev_interactive_state)
self.setTransformationAnchor(prev_anchor_mode)
else:
super(GraphView, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.scaling = False
super(GraphView, self).mouseReleaseEvent(event)
Change this:
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
to that:
self.setTransformationAnchor(QtGui.QGraphicsView.NoAnchor)
setTransformation() is a subject to transformation anchor; AnchorUnderMouse makes sure that a scene point corresponding the current (at the moment when transform is applied) mouse position remains untranslated.

Show CCLabel for few seconds on touch

I'm new to Cocos Sharp and have been struggling to implement behaviour from question title. I want to show message to user that will fade or disappear after some period of time, lats say 3 seconds, after user touches some sprite. But I can't figure it out how to do that.
I'm showing CCLabel like this:
var scoreLabel = new CCLabel("Touch Menu and Exit button together to exit!", "Arial", 50, CCLabelFormat.SystemFont);
scoreLabel.PositionX = button.PositionX;
scoreLabel.PositionY = button.PositionY + 300;
scoreLabel.AnchorPoint = CCPoint.AnchorMiddleRight;
Can someone help me?
After 3 seconds, fadeout in 1 second
scoreLabel.RunActions(new CCDelayTime(3), new CCFadeOut(1));

R gSignalConnect

I'm trying to learn how to signalConnect buttons to labels. For example, I've made a button labeled "+" and created a label initialized to "0". I would like the button to adjust the label by adding one every time the button is clicked. I have constructed the button and the window for it. My question is, how do I use the gSignalConnect() function to perform this?
Thanks,
Joe
-- Here is my code
win <- gtkWindow(show = FALSE)
win$Resize( width = 700, height = 500)
win$SetTitle(title = "Measurement GUI")
frame <- gtkFixedNew()
win$add(frame)
plus<-gtkButtonNewWithLabel("+")
plus$setSizeRequest(80,35)
frame$put(plus, 10, 50)
label <-gtkLabelNew("0")
frame$put(label, 100, 80)
win$ShowAll()
Try
callback <- function(btn, ...) {
old <- as.numeric(label$getText())
label$setText(old + 1)
}
gSignalConnect(plus, "clicked", callback)
To use gSignalConnect, you will need to know which signals (and their signatures) are available for your widget. ?gtkButton will give this information.

Resources