Python Script not responding - python

(I am a total beginner regarding programming)
I have written a python GUI to run batch files that can activate different Gamemodes in CSGO on a local Steam server. When I run the script everything works, but after I execute a command, the Programm does not respond anymore.
import tkinter as tk
import subprocess
def startA():
subprocess.call([r'D:\CSGO_server\Arms_race.bat'])
def startCa():
subprocess.call([r'D:\CSGO_server\Casual.bat'])
def startCo():
subprocess.call([r'D:\CSGO_server\Competitive.bat'])
def startDea():
subprocess.call([r'D:\CSGO_server\Deathmatch.bat'])
def startDem():
subprocess.call([r'D:\CSGO_server\Demolition.bat'])
def startQ():
subprocess.call([r'D:\CSGO_server\Quit.bat'])
root = tk.Tk()
root.title("Settings")
frame = tk.Canvas(root)
frame.pack()
Armsrace = tk.Button(frame,
text="Arms_race",
fg="red",
command=startA)
Armsrace.pack(side=tk.BOTTOM)
Casual = tk.Button(frame,
text="Casual",
fg="red",
command=startCa)
Casual.pack(side=tk.BOTTOM)
Competitive = tk.Button(frame,
text="Competitive",
fg="red",
command=startCo)
Competitive.pack(side=tk.BOTTOM)
Deathmatch = tk.Button(frame,
text="Deathmatch",
fg="red",
command=startDea)
Deathmatch.pack(side=tk.BOTTOM)
Demolition = tk.Button(frame,
text="Demolition",
fg="red",
command=startDem)
Demolition.pack(side=tk.BOTTOM)
Stop = tk.Button(frame,
text="Stop Server",
fg="red",
command=startQ)
Stop.pack(side=tk.BOTTOM)
root.mainloop()
Did I just fotget a loop?(but I have already a loop there, so how would I have to do this then?)
Or is it something completly diffrent?
Thanks in advance
(also sorry for not putting the code as sample code)

did you check your directory if there exists your files or not?

Related

im trying to make a code that edits the priority inside task manager

so what im trying to do here is make the button optimize to set the priority of a an app inside task manager .any idea how? btw im not asking for a code im asking for a method of how to do it
import tkinter as tk
from tkinter import *
def optimize():
MyProgram = tk.Tk()
frame = tk.Frame(MyProgram)
frame.pack()
button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="optimize",
command=optimize)
slogan.pack(side=tk.LEFT)
MyProgram.mainloop()

How to start a second tkinter program from another and then close first?

I'm creating a tkinter gui that I would like to run. This app will do some house cleaning of sorts. It would check files, check for updates, give the user some information etc. Then I would like it to start another tkinter application and then close it self.
import tkinter as tk
import os
def StartProgram():
os.startfile("C:/WINDOWS/system32/notepad.exe")
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame, text="QUIT", fg="red", command= lambda: quit())
button.pack( side=tk.LEFT)
button2 = tk.Button(frame,text="Start my exe", command=lambda: StartProgram())
button2.pack(side=tk.LEFT)
root.maxsize(200,200)
root.minsize(200,200)
root.mainloop()
The problem I have so far is when I attempt to close the original app it closes the one it started as well.
EDIT: I also tried root.destroy(), root.quit()
Any suggestion how I might correct this?

how to close a Thread from Tkinter While the Thread is running Python selenium

So, I made a small canvas window with tkinter which has 2 buttons, One is a start button, the other is a stop button. (I'll attach the GUI tkinter code down below. I wont add the Selenium part because I don't want to confuse anyone with mushed up code.) The start button calls a function thats threaded and that launches my "Reporting_Backbone.py" which is a selenium/pyautogui bot that does a bunch of stuff. My problem is that the stop button does not stop the "Reporting_Backbone.py". In the stop button function I've tried sys.exit() but the selenium and the GUI stay open (and running), I've tried daemons (which I might not have been using them correctly because that did nothing)I've tried setting the stop button function to a lambda (which just freezes the GUI, but not the selenium part) and I've tried setting up some kind of a killswitch as a last resort but honestly this thing wont die, its like Thanos fused with Majin Buu. It just keeps running. How do I make it so that the stop button works? I I'm hoping someone can help me with a solution and explanation. I am still new to coding but I am really loving it, if possible I would really like to understand what I am doing wrong. Thank you.
enter code here
import tkinter as tk
from PIL import Image, ImageTk
import time
import os
import threading
import sys
root = tk.Tk()
#Canvas for GUI
canvas = tk.Canvas(root, width=600, height=800)
canvas.grid(columnspan=3, rowspan=4)
canvas.configure(bg="#b9be9c")
#Button Starting
def start_report():
time.sleep(0.5)
start_text.set("Armed!")
os.system("python Reporting_Backbone.py")
#Button Stopping
def stop_craigslist():
stop_text.set('Stopped')
time.sleep(3)
sys.exit()
#Logo
logo = Image.open('Logo.png')
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
logo_label.image = logo
#playing logo in window
logo_label.grid(column=1, row=0)
logo_label.configure(bg="#b9be9c")
#instructions
instructions = tk.Label(root, text="Click the 'Start' Button to begin.")
instructions.grid(columnspan=3, column=0, row=1)
instructions.configure(font=("Helvetica", 25) ,bg="#b9be9c")
#Start Button
start_text = tk.StringVar()
start_btn = tk.Button(root, textvariable=start_text, command=threading.Thread(target=start_report).start, font=("Helvetica", 18), fg="black", height=2, width=15)
start_text.set("Start")
start_btn.grid(column=1, row=2)
#Stop Button
stop_text = tk.StringVar()
stop_btn = tk.Button(root, textvariable=stop_text, command=threading.Thread(target=stop_craigslist).start, font=("Helvetica", 18), fg="black", height=2, width=15) #If I set this to a lambda function the Tkinter GUI Freezes up on me
stop_text.set("Stop")
stop_btn.grid(column=1, row=3)
root.mainloop()
You cannot stop the task created by threading.Thread(). Use subprocess instead:
import subprocess
...
proc = None
def start_report():
global proc
if proc and not proc.poll():
print("process is still running")
return
proc = subprocess.Popen([sys.executable, "Reporting_backbone.py"])
start_text.set("Armed!")
def stop_craigslist():
global proc
if proc:
proc.terminate()
proc = None
stop_text.set('Stopped')
...
start_btn = tk.Button(root, ..., command=start_report, ...)
...
stop_btn = tk.Button(root, ..., command=stop_craigslist, ...)
...

Connecting the same GUI to two or more python files

i am building a small project which involves 4 python files which have their separate functionalities. There is however, a main.py file which uses all the others by importing them.
Now, i have to build a GUI for this project, which i am building within the main.py file. My problem is that some of the other files have functions which print on the console, when the whole project is run, i want those functions to print on the GUI instead. So, how do i print the text from the other file in a Text field created in the main file.
main.py
import second as s
from tkinter import *
def a():
field.insert(END, "Hello this is main!")
root = Tk()
field = Text(root, width=70, height=5, bd=5, relief=FLAT)
button1 = Button(root, width=20, text='Button-1', command=a)
button2 = Button(root, width=20, text='Button-2', command=s.go)
root.mainloop()
second.py
def go():
print("I want this to be printed on the GUI!")
#... and a bunch of other functions...
I just want that when user presses the button-2, then the function go() prints the text on the field
I consider it the best way to try add field as an argument of function go.
Here is my code:
main.py
import second as s
from tkinter import *
def a(field):
field.insert(END, "Hello this is main!\n")
root = Tk()
field = Text(root, width=70, height=5, bd=5, relief=FLAT)
button1 = Button(root, width=20, text='Button-1', command=lambda : a(field))
button2 = Button(root, width=20, text='Button-2', command=lambda : s.go(field))
#to display widgets with pack
field.pack()
button1.pack()
button2.pack()
root.mainloop()
second.py
from tkinter import *
def go(field):
field.insert(END, "I want this to be printed on the GUI!\n")
print("I want this to be printed on the GUI!")
#something you want
The screenshot of effect of running:)

In Python Tkinter can I press a button to run any exe file?

I have been studying on using Tkinter to open exe files from the one Tkinter software. My end product was a Windows 7 exe file that ran all the exe files from the one Tkinter software that was converted to exe file.
I will explain my code going from top to bottom
This is my Tkinter Template of sorts
from Tkinter import *
from PIL import Image, ImageTk
import os
class App:
def __init__(self, master):
self.frame = Frame(master)
I added a image to give users information How to use the software
img = Image.open("data.gif")
intro = ImageTk.PhotoImage(img)
right = Label(None, image=intro)
right.grid(row=0, column=0, columnspan=4)
right.image=intro
then I added Buttons to the grid, calling every button self.b realy confused people here at stack overflow. you will read the comments soon.
self.b = Button(self.frame, bg="red", fg="white", font=("Helvetica", 14), text = ' \n confilextracter \n ', command = self.openFile1)
self.b.grid(row = 1, column=0)
self.b = Button(self.frame, bg="red", fg="white", font=("Helvetica", 14), text = ' \n confileditor \n ', command = self.openFile2)
self.b.grid(row = 1, column=1)
self.b = Button(self.frame, bg="red", fg="white", font=("Helvetica", 14), text = ' \n confilerehasher \n ', command = self.openFile3)
self.b.grid(row = 1, column=2)
self.b = Button(self.frame, bg="red", fg="white", font=("Helvetica", 14), text = ' \n Turn off the Shed \n ', command = self.openFile4)
self.b.grid(row = 1, column=3)
self.frame.grid()
Next I had to give the buttons jobs to carry out, renaming exe files realy confused people here at stack overflow. sorry about that.
def openFile1(self):
os.startfile("confilextracter.exe")
def openFile2(self):
os.startfile("confileditor.exe")
def openFile3(self):
os.startfile("confilerehasher.exe")
I realy wanted the last button code corrected witch I found the answer myself because everyone else was busy sorting out all the other parts of this code as you will see soon enough. at this point in time this next button has errors in it.
def openFile4(self):
self.b.configure(command = self.b.destroy)
Then I closed the file
root = Tk()
app = App(root)
mainloop()
Using the os module:
from Tkinter import *
import os
class App:
def __init__(self, master):
self.frame = Frame(master)
self.b = Button(self.frame, text = 'Open', command = self.openFile)
self.b.grid(row = 1)
self.frame.grid()
def openFile(self):
os.startfile(_filepath_)
root = Tk()
app = App(root)
root.mainloop()
This is what I did to make things work,
I took f3ar3dlegend example code (scroll up) and started working on it as it worked pretty good.
Lets talk about the top lines of code and work our way down for a full breakdown.
from Tkinter import *
from PIL import Image, ImageTk
import os
from Tkinter import, this tells pyhton to load the GUI drivers. from PIL import Image, ImageTK tell Python to load Pyhton image library so we can use color photos. import os this to my understanding loads drivers so Python can run outside programs from a python app calling them into action.
The first thing I did was to add a image to f3ar3dlegend's code to give my users a information page with this code,
class App:
def __init__(self, master):
self.frame = Frame(master)
img = Image.open("data.gif")
intro = ImageTk.PhotoImage(img)
right = Label(None, image=intro)
right.grid(row=0, column=0, columnspan=4)
right.image=intro
One line of code was stoping me image from apearing for a good 24 hours was just simply missing code
right.image=intro
This line of code stops your image being garbage collected (what ever that means) I just know I added it and my photos should up when put inside a def.
The Next thing I put a lot of work into was Button formating, you know width size color Font. Figgering out to use self.frame was pure guesswork I just kept trying ideas until one of my trys worked. I also have this whole thing on a Python Tkinter Grid so the image went on row 0 and column spaned 4 or 5 colums to make way for more buttons. These Buttons all went on row 1 not row 0. I found I could call all the buttons self.b as long as the command was to a different def the code was error free.
self.b = Button(self.frame, bg="darkred", width=18, fg="white", font=("Arial", 14), text = ' \n confilextracter \n ', command = self.openFile1)
self.b.grid(row = 1, column=0)
self.b = Button(self.frame, bg="red", width=17, fg="white", font=("Arial", 14), text = ' \n confileditor \n ', command = self.openFile2)
self.b.grid(row = 1, column=1)
The next thing I did was def's as buttons don't work without these. What made stock overflow think I was uploading a virus was that I changed the programe names to make me understand my programing better.
def openFile1(self):
os.startfile("confilextracter.exe")
def openFile2(self):
os.startfile("confileditor.exe")
It works like this. openFile1 is a Button callout. You press the button and it gives out a callout and the matching def obeys the anser to the call. os.startfile is a new term to me but it simply means operating system start file. ("confileditor.exe") is saying the string name of the file you need is between (" ") put the two together and the file runs in its own window.
The last bit of this software i peaced together and then said I can do more with this. I started off using f3ar3dlegend example code again.
root = Tk()
app = App(root)
mainloop()
This basicly turns everything off so Python knows it's time to display stuff and mainloop tells Tkinter to wait for me to do stuff. Problem was the software was opening sometime half on the page and at other times anywhere on the screen so I added this code to put the software at the top left of the screen.
app = App(root)
root.geometry('+0+0')
mainloop()
One last thing I did was to get a button to Exit the software at the click of a button after trying 5 or 6 trys I got Tkinter to destroy the software window with the push of a button. the code is.
self.b = Button(self.frame, bg="red", width=18, fg="white", font= ("Arial", 14), text = ' \n Turn off the Shed \n ', command = self.openFile4)
self.b.grid(row = 1, column=3)
def openFile4(self):
root.destroy()
I have seen counless examples on stack overflow of people using root.destory wrongy, is it any wonder I tinkered with it for 1 hour to get it just right.
One very important lesson I did learn about putting programs online so people can download them is You need to contact Customber support of your host and Demand a link to there TOS terms of service because if you don't understand TOS your probably breaking international laws to do with publishing. Thank you.

Categories

Resources