How do I hide a button or simply move it off the screen in VPython?
I have tried:
del button
button.visible = False
button.pos.x = 500 # well off the screen
None of those seem to work.
I figured it out. Open Lib\site-packages\vis\controls.py(in your python folder). Find the button class. Insert this function at the bottom.
class button(ctrl):
...
def _set_visible(self,value):
self.box1.visible = value
self.box2.visible = value
self.box3.visible = value
self.box4.visible = value
self.button.visible = value
self.label.visible = value
def _get_visible(self):
return self.box1.visible
visible =property(_get_visible,_set_visible)
Related
This is a part of a code I've been working on, during the for loop it iterates through product_buttons in order to define which button is selected, and after a button is selected I wanted to show which button was selected and I have managed to do it as follows:
product_buttons = [36,38,40] #raspberry pi pins
in_progress = False
ended = True
product = None
def show_msg(wid,msg):
wid.config(text = msg)
pass
def button_loop():
global in_progress,ended
waiting = False
product = None
while True:
for i in range(len(product_buttons)):
button = IO.input(product_buttons[i])
if button:
print(i,in_progress,ended,product)
if not in_progress:
product = i
while not ended:
time.sleep(0.1)
in_progress = True
show_msg(root.lab_quality,'Button {}
pressed'.format(i))
The code works fine and whenever a button is pressed it changes Button {} pressed with Button 0 pressed, Button 1 pressed or Button 2 pressed.
Now what I was trying to do was to assign a variable name to each iteration in the for loop in order to achieve something like:
i = 0 make i = to a variable for example let's call it a
i = 1 make i = to b
i = 2 make i = to c
So that (still for example) when button 0 is pressed it shows Button a is pressed.
I have tried to store the values like this quality={1:a, 2:b, 3:c} and then call it with show_msg(root.lab_quality,'Button {} pressed'.format(quality)), this was the idea I had in mind but didn't work out and I am a bit stuck and I need to be pointed in the right direction so that I can eventually work it out.
Any help will be greatly appreciated!
You need to define quality as a dictionary like this: quality={'1':'a', '2':'b', '3':'c'}. Then, you can call show_msg function:
show_msg(root.lab_quality,'Button {} pressed'.format(quality[str(i)]))
This way you use the value of i variable as the key to read its name from the quality dictionary.
Thanks guys I have worked it out I did:
btn = ['a','b','c']
show_msg(root.lab_quality,'QUALITY {}'.format(btn[j]))
and works like a charm, was so easy I don't know why I didn't see it before.
I have two buttons on my interface. I want both of them to be able to call their respective functions when I either click on them or a hit the Enter Key.
The problem I'm having is that only the last button in the traveral focus gets activated when I hit the Enter Key, even if the preceeding one has the focus. What can I do to resolve this problem.
Useful answer are welcome and appreciated.
This is the problem in question:
from tkinter import *
w = Tk()
def startProgram(event = None):
print('Program Starting')
def readyContent(event = None):
print('Content being prepared')
# Buttons
Button(text='Prepare', command=readyContent).grid(row=10,column=2)
w.bind('<Return>',readyContent) # Binds the Return key to a Function
Button(text='Start', command=startProgram).grid(row=10,column=3)
w.bind('<Return>',startProgram) # Binds the Return key to a Function
w.mainloop()
When you click on the Prepare or Start button, in return you get either Content being prepared or Program Starting repectively. Nothing like that happens when you use the Tab Key to give focus to one button or the other. Even if the focus is on the Prepare button, when you hit Enter you get: Program Starting
This is the solution to my problem.
I hope it helps anyone else having the same problem as me.
from tkinter import *
w = Tk()
def startProgram(event = None):
print('Program Starting')
def readyContent(event = None):
print('Content being prepared')
# Buttons
btn1 = Button(text='Prepare', command=readyContent)
btn1.grid(row=10,column=2)
btn1.bind('<Return>',readyContent) # Binds the Return key to a Function
btn2 = Button(text='Start', command=startProgram)
btn2.grid(row=10,column=3)
btn2.bind('<Return>',startProgram) # Binds the Return key to a Function
w.mainloop()
Have a good day! :)
I'm trying to create a button that changes colors on click.
After digging around in an old Python book I haven't looked at in years, I've gotten to the point where I make the the button, but I can't figure out how to pass i into the second function so it increments and is then is reset to 0.
I suppose I could just increment I on click in the first function, but now I'm annoyed and want to figure it out.
Instead of self.change_color I tried change_color(i). That threw an error. Same with trying self.change_color(i).
I'm not sure what to do at this point.
import tkinter
class joeGUI:
def __init__(self):
i = 0
colorArray = ['blue','DarkGreen','red','yellow']
self.main_window = tkinter.Tk()
self.color_button = tkinter.Button(self.main_window,
text = 'Click to Change Color',
command = self.change_color,
bg = colorArray[i])
self.color_button.pack()
tkinter.mainloop()
def change_color(self):
if (count < 3):
count += 1
else:
count = 0
return count;
joe_gui = joeGUI()
Store i as a class attribute (self.i = 0) and change the references of count to self.i.
I am trying to create a PyQt Dropdown menu(combo box), whose value I need to pass to another function.
Here is a snippet of my code
def combo_box(self):
combo = QtGui.QComboBox(self)
...#Filled in code here
for i in range(0,len(arr)):
combo.addItem(arr[i])
#I want to get the value of the selected Drop-down content to be stored in value
value = combo.activated[str].connect(self.ComboValue)
print value
def ComboValue(self,Text):
print Text
return Text
When I print the variable Text in the ComboValue method it prints it right, but when I print value from the combo_box method it prints None.
I wanted to know why this happens, and is there an alternative to return the value to the other method?
combo.activated[str].connect(self.ComboValue) is signal and signal never return you anything back, so that's why you getting None. Take a look http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html
You may use this value = str(combo.currentText()), but don't know you want this.
And change combo.activated[str].connect to combo.currentIndexChanged [str].connect to get your values properly
create the combobox, then wait till user activates an item:
def combo_box(self):
combo = QtGui.QComboBox(self)
...#Filled in code here
for i in range(0,len(arr)):
combo.addItem(arr[i])
combo.activated.connect(self.ComboValue)
# save it and wait for user to do something:
self.combo = combo
def ComboValue(self, Text):
# user has selected an item, save it:
self.value = Text
assert combo.currentText() == Text
... do something with it ...
Not tested so details may be wrong.
I am wondering how to get my code to work. I have a class wich creates a popup window with buttons. Each button should be bound to subclass. But it doesnt work. What´s wrong with my code?
class chooser:
def __init__(self):
None
def show(self,title,options=["NOTHING"],size=(.5,.5)):
self.bts = {}
self.response = False
self.content = FloatLayout()
self.content.pos_hint = {"y":0,"x":0}
# create buttons
pos_cntr = 0
for opt in options:
self.bts[pos_cntr] = Button(text=opt)
self.bts[pos_cntr].size_hint = 1,float(1)/float(len(options))
self.bts[pos_cntr].pos_hint = {"x":0,"y":pos_cntr}
self.bts[pos_cntr].bind(on_press=self.canceldia)
self.content.add_widget(self.bts[pos_cntr])
print "bound"
pos_cntr += float(1)/float(len(options))
self.pop = Popup(title=title,content=self.content,auto_dismiss=False)
self.pop.size_hint = size
self.pop.open()
def canceldia(self,instance):
print "closing"
self.response = instance.text
self.pop.dismiss()
def getresponse(self):
return self.response
I have imported all needed modules.
I execute it so:
c = chooser()
c.show("hello","world",["welcome","close","nothing","example"])
I have create a root widget. The popup works fine and all is created nice but the buttons are not bound. Please help me!
In your loop, you always reference self.bts[pos_cntr], so you override it in every iteration. How about this?
for idx, opt in enumerate(options):
self.bts[idx] = Button(text=opt)
self.bts[idx].size_hint = 1,float(1)/float(len(options))
self.bts[idx].pos_hint = {"x":0,"y":pos_cntr}
self.bts[idx].bind(on_press=self.canceldia)
self.content.add_widget(self.bts[idx])