Executing a set of codes when python GUI is clicked - python

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.

Related

How to unite 2 scripts in one using tkinter?

I'm making an tkinter app. I have 2 scripts and I have to make an GUI where you can press one button and scripts will be run in sequence. Also I should note that my scripts also use tkinter.
I tried to make it just by making 2 functions for each script and putting them in one button, but it doesn't work right. It's only open window of first script and second one starts only if I close both open windows.
This is what i tried
import tkinter as tk
from tkinter import ttk
def run_script_1():
# Code for running script 1
print("Running script 1")
def run_script_2():
# Code for running script 2
print("Running script 2")
def run_both_scripts():
run_script_1()
run_script_2()
root = tk.Tk()
root.title("Script Runner")
root.geometry('600x100')
frame = ttk.Frame(root)
frame.pack()
button = ttk.Button(frame, text="Run Both Scripts", command=run_both_scripts)
button.pack()
root.mainloop()

GUI opens when i double cklick the .py file, but doesn't when i run it in pycharm

from tkinter import *
root = Tk()
myLabel = Label(root, text='Hello World!')
myLabel.pack()
root.mainloop()
When i try to run it with the play button in pycharm nothing happens, but when i copy it into the Python Console it opens a GUI
Also works if i double cklick the file.
Why is nothing happening if i try to run it with the button ?
i've copied this code from a YT tutorial: https://www.youtube.com/watch?v=yQSEXcf6s2I&list=PLCC34OHNcOtoC6GglhF3ncJ5rLwQrLGnV
I have the tendency to just, right click the file on PyCharm and click on the run option.
Because, when you directly click on the run button, it plays all your files onpened on Pycharm, so if you have more than one file it's not great...

GUI MDI Subwindow Only shows on first Button Press

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()

Tkinter: when clicking on button GUI is frozen

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!

Run function from Python 2 Tkinter button when pressed then run another when released

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()

Categories

Resources