I have a script that enables a Power supply after the user click "ONLY ONE TIME" the "START TEST" button, right after, I disable the button to avoid the "double click, however, I have noticed that "some how" if user perform a "double click" my application launch a second operation.
def starttest():
Button(main, text="START TEST", font=("Tahoma", 21), height=1, width=24,
command=starttest, state=DISABLED).place(x=55,y=40)
PS = serial.Serial('COM1', 9600, timeout=.1)
##my code here ...
Does anyone have an idea what should I do to prevent this happen?
Each time you call starttest() function by pressing the inherent button, a new button widget is created: that is why it looks like you are able to click indefinitely on the falsely "same" button.
You should create that button somewhere else in your program:
...
power_supply_btn = Button(... text="START TEST", command=starttest ...)
power_supply_btn.place(x=55,y=40)
...
Then configure the state of that button in your starttest() callback using the configure() function:
def starttest():
global power_supply_btn
power_supply_btn.configure(state=DISABLED)
Related
I've function as a edit_parameters when I changed value on my edit_table. When I open the edit table screen edit_parameters function working.
def edit_parameters(self):
self.ui.edit_table.setEditTriggers(QtWidgets.QTableWidget.CurrentChanged)
oldListValue = []
for i in range(0, 46):
oldListValue.append(self.ui.edit_table.item(i, 1).text())
self.ui.changeButton.clicked.connect(partial(self.write_changed_value_to_device,0,oldListValue))
Than I define changeButton in above function, so I expect when I click this button, write_changed_value_to_device calling once. But when I click the button sometimes this function calling more than once
def on_write_changed_value_to_device(self,changeType,oldListValue = []):
print("changed")
When I click once, I see:
changed
changed
changed
Why this happenning I want to call only once when I click the button.
I'm automation a notepad GUI developed in PYQT5 using PYtestqt. when I try to click the menu bar or toolbar options using qtbot it is not simulating the click
def test_quit(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
qtbot.wait_for_window_shown(window)
qtbot.mouseClick(window.file_menu, QtCore.Qt.LeftButton)
I was trying to find a way to trigger an action under a menu. Since you most likely want to trigger an action (item under the menu), this may help you as well. Instead of using qtbot, use the window directly and call trigger. So something like this:
def test_quit(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
qtbot.wait_for_window_shown(window)
window.file_quit_action.trigger()
if you wanna just trigger the click event connected action, you can try this.
In MainWindow class:
# binding action and function
action_open = QAction("Open", self)
action_open.setCheckable(False)
action_open.setObjectName("action_open") # Notice set a object name
action_open.triggered.connect(self.file_open)
In test scripts:
def test_file_open_action(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
win.findChild(QAction, 'action_open').trigger() # call the method
So you can get the same result after simulate click memubar widget.
I wan to create a file for my program that users open up and choose their options before running the calculation in a VI or other editor. Kind of like the menu in VI where you can highlight a word and expand/change to that topic. Only I want to let then highlight an option and see all the possible choices and be allowed to choose one. Is there a way to create such a file/capability?
Not certain what you mean, but you could create a simple GUI in Python like this that lets your users select parameters from lists and then writes the "settings" file for the FORTRAN program and starts it when a button is pressed:
#!/usr/local/bin/python3
from guizero import App, ListBox, Text, PushButton
def StartThatPuppy():
print("Starting FORTRAN...")
# Write currently selected settings to config file
# subprocess.call('/Users/fred/program.exe')
app = App(title="FORTRAN Parameter Setup", width=300, height=200)
t=Text(app, text="Select optimisation method")
listbox = ListBox(app,
items=["Method A", "Method B", "Method C (beta)", "Naive Method"],
selected="Method B",
scrollbar=True)
listbox.height=3
listbox.width=20
t=Text(app, text="Select threshold")
listbox = ListBox(app,
items=["1%", "2%", "5%", "10%"],
selected="5%",
scrollbar=True)
listbox.height=3
listbox.width=20
button = PushButton(app, text="Start FORTRAN", command=StartThatPuppy)
app.display()
That looks like this:
More examples and documentation are here.
Alternatively, if you want something textual, without needing a GUI, you could use whiptail like raspi-config uses on the RaspberryPi:
Another option might be dialog, documentation here
That could look like this:
dialog --title "FORTRAN Parameter Setup" "$#" \
--checklist "You can choose your threshold here.\n\
Press SPACE to toggle an option on/off. \n\n\
What threshold do you want?" 20 61 5 \
"1%" "Default threshold" ON \
"2%" "Less sensitive" off \
"5%" "Can be noisy" off \
"Unlimited" "Potentially too many results" off
retval=$?
I want to show the text from the 'Yes' and the 'No' button in the informative text of a QMessageBox, but I don't see how I can get these labels from the buttons.
from PyQt5.QtWidgets import *
import sys
app = QApplication(sys.argv)
msgbox = QMessageBox()
msgbox.setStandardButtons(msgbox.Yes | msgbox.No)
info_text = "Click '{yes}' to confirm. Click '{no}' to abort."
msgbox.setInformativeText(info_text)
if msgbox.exec_() == msgbox.Yes:
print("Confirmed")
else:
print("Aborted")
By calling setStandardButtons, the button order and the button labels will be set to the default for the current operating system and the current language setting. How can I obtain these defaults so that I can use them for the slots in the string info_text?
I thought about using the buttons attribute from the QMessageBox object, which is a list of QPushButton objects. I can read the labels from there, but I don't see how I could determine whether the first element in the list is the Yes or the No button.
Okay, I was being stupid: along side the buttons attribute, there is also the button() method, which takes as its argument the button type that I want to retrieve. I can then use text() to get the label. Finally, the hotkey marker & has to be stripped from the label:
info_text = "Click '{yes}' to confirm. Click '{no}' to abort.".format(
yes=msgbox.button(msgbox.Yes).text().replace("&", ""),
no=msgbox.button(msgbox.No).text().replace("&", ""))
msgbox.setInformativeText(info_text)
I have eighteen buttons that need to all change from one image to another upon the press of another button. I could just call .configure on each and set it that way, however, I feel as though there is a much cleaner simpler way. Any ideas?
If the buttons are all in a list, you can loop over them, like this:
self.buttons = [button1, button2, ..., button18]
def updateButtonImage(self):
for button in self.buttons:
button.configure(image=self.newImage)
updateButton = Button(root, text="Change button image", command=self.updateButton)
Is that what you had in mind?