I am trying to create a button in GTK+ that has the shape of a Hexagon.
How can I do it without using CSS?
And more general, how can I create my button in any shape I want?
Is it possible to do such things in Glade(User interface edior for GTK+)?
When I put my comment I was bluffing because I never did the circular button.. I just did an example of an Hexagonal button using my original idea.. I got surprised that it was simpler than I thought!
( and by the way, no, it is not possible to do it in glade! )
# sorry but the example is in Python! :/
from gi.repository import Gtk
def hexagon(coord_x, coord_y):
# because of the symetry I take only the absolute value
coord_x, coord_y= abs(coord_x), abs(coord_y)
# I got the constants by clicling in the image and printing the coord_x and coord_y values
if coord_x <= 13 and coord_y <= 25: # define a rectangle
return True
else:
# I cut the coord x to define a triangle
coord_x=coord_x-13/2
# line equation
ymax=(-25/31)*coord_x+25
if coord_y < ymax:
return True
else:
return False
class GUI(Gtk.Window):
def __init__(self):
self.window_root=Gtk.Window()
# Create an event box to handle the click's
self.eventbox=Gtk.EventBox()
self.eventbox.connect('button-press-event' , self.on_eventbox_pressed)
self.eventbox.connect('button-release-event' , self.on_eventbox_released)
# Load the images
self.hexagon1=Gtk.Image.new_from_file('./3uSFN.png')
self.hexagon2=Gtk.Image.new_from_file('./cWmUA.png')
# init the event box
self.eventbox.add(self.hexagon1)
self.window_root.add(self.eventbox)
self.window_root.show_all()
# a variable to store the state of the button
self.state=False
def on_eventbox_pressed(self, widget , event):
if 'GDK_BUTTON_PRESS' in str(event.type): # If the user made a "single click"
if event.button == 1: # if it is a left click
# get the x,y of the mouse from the center of the image
pos_x, pos_y=self.window_root.get_position()
siz_x, siz_y=self.window_root.get_size()
mouse_x,mouse_y=event.x-siz_x/2, siz_y/2-event.y
if hexagon(mouse_x, mouse_y):
self.eventbox.remove(self.hexagon1)
self.eventbox.add(self.hexagon2)
self.eventbox.show_all()
self.state=True
def on_eventbox_released(self, widget , event):
if self.state:
self.eventbox.remove(self.hexagon2)
self.eventbox.add(self.hexagon1)
self.state=False
main=GUI()
Gtk.main()
I think that the only inconvenient of using this to solve your problem is that the theme of the user is not respected. If that is a problem, instead of using an image you could draw your button by using a DrawingArea and by getting the theme colors as I suggested here!
I hope it be useful :)
Related
I have a plugin window is QDialog, but I want to add QMenubar or QStatusBar
I have tried to find a way to convert QDialog to QMainWindow, but there is no result,
So I try to add QMenubar in QDialog again and everything works fine
Until the position of the object I added to the layout screen ran to the upper left corner.
I am not a native English speaker
def run(self):
"""Run method that performs all the real work"""
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
#建立menuBar的父節點物件
file_menu = QMenu("File")
#建立一個
open_action = QAction("Open",file_menu)
open_action.setShortcut('Alt+F4')
#把剛才建立的open action 加入file 節點
file_menu.addAction(open_action)
#建立一個menubar物件
menubar = QMenuBar(self.dlg)
#將父節點加入menubar
menubar.addMenu(file_menu)
#將self.dlg的menubar設定為menubar物件
self.dlg.setMenuBar(menubar)
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
pass
I have an array with lights , everytime I create a light it stores is my array.
I have a textScrollList that displays all lights in my Array.
When I add lights , it doesn't refersh the textScrollList.
Can someone tell me how I can do this , so everytime I make a light it display it in the textScrollList. Or with a refresh button.
Thanks !
Code I have now :
import maya.cmds as cmds
lights=[]
myWindow = cmds.window(title='My Lights', wh=(200,400),sizeable =False )
cmds.columnLayout()
cmds.showWindow(myWindow)
LightsButton = cmds.button(label='Make Lights', command = "makeLights()", width =200,height = 25,align='center')
def makeLights():
lights.append(cmds.shadingNode('aiAreaLight', asLight=True))
LightSelector = cmds.textScrollList( numberOfRows=8, allowMultiSelection=True,append=(lights), showIndexedItem=4, selectCommand = 'selectInTextList()' )
You can add a function that will refresh the list with the lights. This can be called after you create a new light so it adds to the list. You can also add a refresh button to call this same function in case you add/delete lights in the scene, and it will update properly.
You don't need to add the lights to a list and keep track of it. Instead you can use cmds.ls() to collect all the lights in the scene. Unless you really do need the list for some reason, it's easy to edit the example below to use it:
import maya.cmds as cmds
# Clear the listview and display the current lights in the scene.
def refreshList():
# Clear all items in list.
cmds.textScrollList(lightSelector, e=True, removeAll=True)
# Collect all lights in the scene.
allLights = cmds.ls(type='aiAreaLight')
# Add lights to the listview.
for obj in allLights:
cmds.textScrollList(lightSelector, e=True, append=obj)
# Create a new light and add it to the listview.
def makeLights():
lights.append(cmds.shadingNode('aiAreaLight', asLight=True))
refreshList()
def selectInTextList():
# Collect a list of selected items.
# 'or []' converts it to a list when nothing is selected to prevent errors.
selectedItems = cmds.textScrollList(lightSelector, q=True, selectItem=True) or []
# Use a list comprehension to remove all lights that no longer exist in the scene.
newSelection = [obj for obj in selectedItems if cmds.objExists(obj)]
cmds.select(newSelection)
# Create window.
myWindow = cmds.window(title='My Lights', wh=(200,400), sizeable=False)
cmds.columnLayout()
cmds.showWindow(myWindow)
# Create interface items.
addButton = cmds.button(label='Make Lights', command='makeLights()', width=200, height=25, align='center')
lightSelector = cmds.textScrollList(numberOfRows=8, allowMultiSelection=True, append=cmds.ls(type='aiAreaLight'), showIndexedItem=4, selectCommand='selectInTextList()')
refreshButton = cmds.button(label='Refresh list', command='refreshList()', width=200, height=25, align='center')
Hope that helps.
I am currently working with a Nao robot using Choregraphe and am trying to lower the confidence interval required to act upon a request made through QiChat from the default 50% to 30%.
I have found this solution, https://community.ald.softbankrobotics.com/en/forum/change-speech-engine-confidence-threshold-choregraphe-dialog-8624, but unfortunately the scripting functionality for Dialog boxes is deprecated in Choregraphe v2.1. Does anyone know what the "new" way to do this is?
I have found the solution. Scripting for Dialog boxes is not allowed but you can add a Python script before the Dialog box to change this interval. The code that should go in this box is below.
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
#put clean-up code here
pass
def onInput_onStart(self):
# Lower confidence threshold from 50% to 30%
ALDialog = ALProxy('ALDialog')
ALDialog.setASRConfidenceThreshold(0.3)
self.onStopped() #activate the output of the box
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box
Two solutions to increase recognition rate:
1) Add more variants to your input - for example, if you're listening for "yes", you should also make sure you listen for "yep", "yup", "yeah", "sure", "okay", "fine", etc. - concepts are useful for that, see the qichat doc.
1) as you suggest, set the confidence threshold - for a more compact version (I prefer less boilerplate):
class MyClass(GeneratedClass):
def onInput_onStart(self):
# Lower confidence threshold from 50% to 30%
ALProxy('ALDialog').setASRConfidenceThreshold(0.3)
self.onStopped() # activate the output of the box
HOWEVER, note that this is not very elegant; you will need to reset it, and it greatly increases the risk of false positives, so you should only use this if you can't solve it just by adding more variants.
setASRConfidenceThreshold is for Nao V5; in Pepper and Nao V6 you should use setConfidenceThreshold:
class MyClass(GeneratedClass):
def onInput_onStart(self):
# Lower confidence threshold from 50% to 30%
ALProxy('ALDialog').setConfidenceThreshold("BNF", 0.3)
self.onStopped() # activate the output of the box
Im currently building fps-like game for android environment.
I had notice that if I make object with use of pixel low resolution devices can play game so easy than hight resolution phones.
If I use percentage for building objects this time bigger devices gain advantage. Such as Tablets have great size than phones and they can shot my object easly.
I want my objects exact same size on every device is it possible?
More specificly I use python-kivy is it possible to define object as cm/ft or etc.
You can make a relative_size and relative_position method. And make them relative to the windows width or height.
You get the size of the window from the Window class.
Remember only to make the objects size (w,h) relative to only one of width or height. Or your objects will be warped.
from kivy.uix.widget import Widget
from kivy.graphics import Canvas,InstructionGroup,Color,Ellipse
from kivy.core.window import Window
from kivy.app import App
class MyCanvas(Widget):
def __init__(self,**kwargs):
super(MyCanvas,self).__init__(**kwargs)
#Window.size = (200,100)
self.size = Window.size
self.orientation = "vertical"
self.ball = InstructionGroup()
self.ball_size = self.relative_size(10,10)
self.color = Color(0, 1, 1)
self.ellipse = Ellipse(size=self.ball_size,pos=self.relative_position(100,50,self.ball_size))
self.ball.add(self.color)
self.ball.add(self.ellipse)
self.canvas.add(self.ball)
def relative_position(self,x,y,obj_size=(0,0)): # pass obj_size if you want the position to be the center of the object11
x = ( self.width / 100.0 ) * x - obj_size[0]/2.0
y = ( self.height / 100.0 ) * y - obj_size[1]/2-.0
return (x,y)
def relative_size(self,w,h):
return (self.width/float(w),self.width/float(h)) # only make it relative to your width or heigh
# or your object will be warped
class MyApp(App):
def build(self):
return MyCanvas()
if __name__ == "__main__":
MyApp().run()
The relative_position method here will now be a percantage. So you pass from 0-100 in both directions. If you want something else, change the 100s in tht method.
Try to uncomment the #Window.size = (200,100) and play with the window size, and see how it works.
You could also make an event if your application changes size, like if your phone changes orientation.
As I did not make that, this will only work for the size the application started with.
I have searched online and wasn't able to find an answer to this so I figured I could ask the experts here. Is there anyway to get the current window resolution in OpenCV? I've tried the cvGetWindowProperty passing in the named instance of the window, but I can't find a flag to use.
Any help would be greatly appreciated.
You can get the width and height of the contents of the window by using shape[1] and shape[0] respectively.
I think when you use Open CV, the image from the camera is stored as a Numpy array, with the shape being [rows, cols, bgr_channels] like [480,640,3]
code e.g.
import cv2 as cv2
cv2.namedWindow("myWindow")
cap = cv2.VideoCapture(0) #open camera
ret,frame = cap.read() #start streaming
windowWidth=frame.shape[1]
windowHeight=frame.shape[0]
print(windowWidth)
print(windowHeight)
cv2.waitKey(0) #wait for a key
cap.release() # Destroys the capture object
cv2.destroyAllWindows() # Destroys all the windows
console output:
640
480
You could also call getWindowImageRect() which gets a whole rectangle: x,y,w,h
e.g.
import cv2 as cv2
cv2.namedWindow("myWindow")
cap = cv2.VideoCapture(0) #open camera
ret,frame = cap.read() #start streaming
windowWidth=cv2.getWindowImageRect("myWindow")[2]
windowHeight=cv2.getWindowImageRect("myWindow")[3]
print(windowWidth)
print(windowHeight)
cv2.waitKey(0) #wait for a key
cap.release() # Destroys the capture object
cv2.destroyAllWindows() # Destroys all the windows
-which very curiously printed 800 500 (the actual widescreen format from the camera)
Hmm... it's not really a great answer (pretty hack!), but you could always call cvGetWindowHandle. With that native window handle, I'm sure you could figure out some native calls to get the contained image sizes. Ugly, hackish, and not-very-portable, but that's the best I could suggest given my limited OpenCV exposure.