I am new using Tkinter to create GUIs. I would like to create a GUI which has a button that when pressed it executes some command lines.
This is the code for creating the button:
ros_start_button = Button(bottom_frame, text="Start ROS", command=self.start_ROS)
ros_start_button.pack(side=LEFT)
the code fro self.start_ROS is
def start_ROS(self):
os.system("bash start_ROS.sh")
where start_ROS.sh is an sh file which code is:
echo "Starting ROS";
cd;
source /home/m/PycharmProjects/ROSAutonomousFlight/catkin_ws/devel/setup.bash;
roslaunch ardrone_numeric_method_controller ardrone.launch;
When I press the button it is like the GUI is frozen until I stop the process that is executed by roslaunch .... I would like to know what can I do in order to just press the button, but not get the GUI frozen because I have other buttons that have to be pressed after this button.
Thank you in advance!
Related
Alright, so all I want is to wait for a function to be run and then continue the rest of the code. Basically, all I want to do is wait until the user presses a Tkinter button, and then after that start the rest of the code.
from tkinter import *
def function():
[insert something here]
root=Tk()
btn1=Button(root, command=function)
Now wait until the user presses the button and then continue
print("Yay you pressed a button!")
If you want to continue some code after pressing button then put this code inside function()
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def function():
print("Yay you pressed a button!")
# ... continued code ...
# --- main ---
root = tk.Tk()
btn1 = tk.Button(root, command=function)
btn1.pack() # put button in window
root.mainloop() # run event loop which will execute function when you press button
PEP 8 -- Style Guide for Python Code
EDIT:
GUIs don't work like input() which waits for user data. Button doesn't wait for user's click but it only inform mainloop what it has to display in window. And mainloop runs loop which gets key/mouse events from system and it checks which Button was clicked and it runs assigned function. GUI can't stops code and wait for button because it would stop also other widgets in window and it would look like it frozen.
The same is in other GUIs - ie. PyQt, wxPython, Kivy - and other languages - ie. Java, JavaScript.
I'm working on a Tkinter GUI that displays matplotlib figures. I use the variable 'window' to initialize the Tk interpreter; instead of 'root' / 'main' = Tk(). The GUI is formatted by the class MyWindow.
class MyWindow:
def __init__(self, win):
Three GUI 'visual' buttons display matplotlib figures. The functions for the mpl figures are in a module called 'charts'. In the below example, 'overview' is a visual button.
self.btn5=Button(win, text='Overview')
self.b5=Button(win, text='Overview', width='6', height='2', command=self.overview)
self.b5.place(x=50, y=160)
def overview(self):
from charts import overview
overview()
A 'quit' button exits the visual windows and the GUI itself. Quit button code:
self.btn6=Button(win, text='Quit')
self.b6=Button(win, text='Quit', width='6', height='2', command=window.quit)
self.b6.place(x=50, y=230)
def quit(self):
self.win.destroy()
All features work as intended, barring the 'quit' button. I must click 'quit' as many times as the number of windows the GUI opened for it to work.
i.e. I launch the GUI and open the 'overview' visual; I need to click 'quit' twice to close 'overview' and the GUI itself.
I've tried adjusting both the quit button 'command' and the quit function to all combinations of destroy() / quit() with prefixes self, win, and window (and no prefix).
Try to use Python inbuilt function called quit() it will just quit the program killing its processes.
def quit(self):
quit()
I hope I have a simple question here. I have created a very large GUI with QT Designer and a subwindow for the MDI area. I have used pyuic5 to convert it from a .ui file to a .py file. I have written a function to open that subwindow when a button is pressed. The first time I push the button it works fine. The problem I'm having is the second time the button is pushed it just displays a blank subwindow within the MDI area. How do I get it to display correctly every time the button is pressed. I will attach the code for how I launch the subwindow below. Any advice would be very appreciated. Thank you for your time and your help
Code that is called when button is clicked
def windowaction(self):
sub = QtWidgets.QMdiSubWindow()
sub.setWidget(self.Load_Input)
sub.setObjectName("Load_Input_window")
sub.setWindowTitle("Load Input")
self.mdiArea.addSubWindow(sub)
sub.show()
First time clicking the button
Second time clicking the button
The problem arises from adding the same widget object to different QMdiSubWindow, you must create a new object and add it to the new QMdiSubWindow.
def windowaction(self):
sub = QtWidgets.QMdiSubWindow()
Load_Input = LoadInput()
sub.setWidget(Load_Input)
sub.setObjectName("Load_Input_window")
sub.setWindowTitle("Load Input")
self.mdiArea.addSubWindow(sub)
sub.show()
Hello I am trying to make a simple recorder in Python 2.7 using Tkinter as the GUI, I want to be able to record when the button is pressed then save the recording when the button is released, I know how to make the button and have already done so, but I don't know how to make it run a program when pressed and another when released, is it possible?
Also I'm not sure how to actually record from the microphone and save it using pyaudio, any help with this is appreciated but I'm sure I can figure this out myself when I have overcome the main issue.
You can bind an event to the click of the left mouse button <Button-1> and to the release of the left mouse button <ButtonRelease-1>. Here's an example:
import Tkinter as tk
root = tk.Tk()
def clicked(event):
var.set('Clicked the button')
def released(event):
var.set('Released the button')
var = tk.StringVar()
var.set('Nothing to see here')
label = tk.Label(root, textvar=var)
label.pack()
but = tk.Button(root, text='Button')
but.bind("<Button-1>", clicked)
but.bind("<ButtonRelease-1>", released)
but.pack()
root.mainloop()
It's my first time trying out Python's GUI and I've decided to go with Tkinter. I have a function in my script which would convert all .txt files in a folder to .xml files. But I would like to create a GUI button which would run my function only if I were to click the button. If I don't click the button, then the files would not be converted at all. How should I do this?
Use the command option of a Button:
from Tkinter import Tk, Button
root = Tk()
def func():
'''Place code to convert files in here'''
print "Button has been pushed"
Button(text="Push me", command=func).grid()
root.mainloop()
func will only run when the button is pressed.