skip to Main Content

Sorry if the title is not accurate to what I am trying to do. It was the best way of explaining my problem at the time.

Okay so on to my problem:

I am trying to create a Tkinter program that when a button is clicked a program that I have created will run in the background and will not stop the user from using the Tkinter program. (Sorry, I really hope you understand this) The closest thing I can think of, that is doing something similar to what I am trying to do, is when you click the "scan this computer" on your anti-malware. It runs the scan but still allows the user to click cancel and go about the program if they want. I am able to get the program to run the command but every time it focuses all of its’ attention on the scan and I (the user) can no longer browse about the program.

I have tried using multi-threading but I cant tell if I am supposed to use that for the program or if I am supposed to use multi-processing, I don’t know.

(I don’t know if when you paste all of this into a file if it will work or not)
I will try to post as much of the code as possible:

Main Script

#!/usr/bin/python3
from tkinter import *
from tkinter.ttk import *
import subprocess as sub
from sys import platform
from mmfunctions import *
# import time
from threading import *
import _thread
# Global variables
OS = ''




def osdetect():
    global OS
    if platform == 'Ubuntu' or platform == 'Debian':
        depend()
        OS = platform
    elif platform == 'darwin':
        print('This is a mac, not all functions will work')
        OS = platform
    elif platform == 'win32':
        print('This is a windows machine, not all functions will work')
        OS = platform


def rmvusrgrubutton():
    scrip = scriptrunnerGUI()
    print(scrip.usrd)
    scrip.usrd = scrip.usrd + 1
    print(scrip.usrd)
    print('Removing User / Group Settings buttons')
    scrip.usrgru()


def rmvmmbuttons():
    scrip = scriptrunnerGUI()
    print(scrip.mmd)
    scrip.mmd = scrip.mmd + 1
    print(scrip.mmd)
    print('Removing main menu buttons...')
    scrip.mmenu()


def depend():
    command = 'sudo apt install screen -y'  # installs required dependancies
    sub.call(command.split())
    print('Screen has been installed due to not already being installed. Screen is required to run most of the commands in this script.')


class runThreadFunc():
    #def __init__(self, ThreadID, name):
    #    threading.Thread.__init__(self)
    #    self.threadID = ThreadID
    #    self.name = name

    def run(self):
        print('Starting ' + self.name)


    def threaderSRCH(self):
        scrip = scriptrunnerGUI()
        # print(scrip.comtorun)
        # command = scrip.comtorun
        #try:
        #self.threaderRun = _thread.start_new_thread(mmfunc.srchmedia, ("Thread search media", 1))
        self.threaderRun = Thread(target = mmfunc.srchmedia)
        self.threaderRun.start()
        self.threaderRun.join()
        self.threaderRun.isAlive()
        #except:
        #    print('Could not start thread')



# threadmmFunc(command):
#    try:
#        print(command)
#        _thread.start_new_thread(command)
#    except:
#        print('Could not start thread')


class scriptrunnerGUI():
    ssh = ''
    ftp = ''
    proftpd = ''
    vsftpd = ''
    web = ''
    apaweb = ''
    nginweb = ''
    smb = ''
    sql = ''
    rsnc = ''
    buttons = []
    usrgrubuttons = []
    usrd = 0
    mmd = 0


    def __init__(self):
        # buttonNames=[]
        self.header = Label(text='First Time Configuration')
        self.header.config(font=24, background='lightblue')
        self.header.grid(row=0, sticky='W')
        self.qalbl = Label(text='Does your system require the following services?')
        self.qalbl.config(font=14, background='lightblue')
        self.qalbl.grid(row=1)

        # Box Labels
        self.sshlbl = Label(text='SSH [y/n] : ')
        self.sshlbl.config(background='lightblue')
        self.sshlbl.grid(row=2, column=0, sticky='W', pady='20')
        self.ftplbl = Label(text='FTP [y/n] : ')
        self.ftplbl.config(background='lightblue')
        self.ftplbl.grid(row=3, column=0, sticky='W')

        # Entry/Checkbuttons Boxes

        #self.ssh1 = Checkbutton(root, text='yes', offvalue=0, onvalue=1)
        #self.ssh1.config()
        #self.ssh2 = Checkbutton(root, text='no', offvalue=0, onvalue=1)
        #self.ssh2.config()
        #self.ssh1.grid(row=2, column=0, sticky='W', padx='60')
        #self.ssh2.grid(row=2, column=0, sticky='W', padx='100')

        self.ftp1 = Entry(textvariable=self.ftp)
        self.ftp1.grid(row=3, column=0, sticky='W', padx='60')


        # self.mmenu()


    def mmenu(self):
        print('In Main Menu')
        self.header = Label(text='Main Menu')
        self.header.config(font=18, background='lightblue')
        self.header.grid(row=0)

        buttonNames=['Search For Prohibited Media', 'Updates', 'User / Group Settings', 'Firewall Settings', 'Services Settings', 'Malware Removal', 'Audit System Using Lynis (linuxOnly)', 'Basic Configurations', 'Remove Prohibited Software']
        thred = runThreadFunc()

        commands = [thred.threaderSRCH, mmfunc.updates, self.usrgru]
        gridrow = ['5', '1', '1', '2', '2', '3', '4', '4', '3']
        gridcolumn = ['0', '0', '1', '0', '1', '1', '0', '1', '0']
        # buttons=[]

        # alyn function will not work until all other functions before it works

        for i in range(0, 3):
            #command = self.commandsmm[i]
            self.buttons.append(Button(root, text=buttonNames[i], width='40', command=commands[i]))
            self.buttons[i].grid(row=gridrow[i], column=gridcolumn[i], pady='2', padx='5')
            #self.ctr.append(i)

        for i in range(3, 9):
            self.buttons.append(Button(root, text=buttonNames[i], width='40'))
            self.buttons[i].grid(row=gridrow[i], column=gridcolumn[i], pady='2', padx='5')

    def usrgru(self):
        print('In Users and Group Settings')
        buttonNames = ['Back to Main Menu', 'Add User to System', 'Remove User from System', 'Add Group to System', 'Remove Group from System', 'Add User to Group', 'Remove User from Group', 'List Local Users', 'List Local Groups', 'List Members of Group', 'List the Groups an User is in', 'Change all Users Passwords at Once']
        commands = [rmvusrgrubutton]
        gridrow = ['7', '1', '1', '2', '2', '3', '3', '4', '4', '5', '5', '6']
        gridcolumn = ['0', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1', '0']

        self.header2 = Label(root, text='User and Group Settings')
        self.header2.config(font=18, background='lightblue')
        if self.usrd == 1:
            print('Destroying Header')
            self.header2.grid_remove()
        else:
            print('usrd != 1')
            self.header2.grid(row=0)

        for i in range(0, 1):
            self.usrgrubuttons.append(Button(root, text=buttonNames[i], width='40', command=commands[i]))
            if self.usrd == 1:
                print('Destroying Buttons')
                self.usrgrubuttons[i].grid_remove()
            else:
                print('usrd != 1')
                self.usrgrubuttons[i].grid(row=gridrow[i], column=gridcolumn[i], pady='2', padx='5')

        for i in range(1, 11):
            self.usrgrubuttons.append(Button(root, text=buttonNames[i], width='40'))
            if self.usrd == 1:
                print('Destroying Buttons')
                self.usrgrubuttons[i].grid_remove()
            else:
                print('usrd != 1')
                self.usrgrubuttons[i].grid(row=gridrow[i], column=gridcolumn[i], pady='2', padx='5')

###################################################


if __name__ == '__main__':
    osdetect()
    # creation of GUI
    root = Tk()
    root.title('Apple CIDR Script Runner')
    if platform == 'win32':
        root.geometry("565x300")
    else:
        root.geometry("735x300")
    frameMain = Frame(root)
    frameMain.rowconfigure(1, weight=0)
    frameMain.columnconfigure(1, weight=1)
    # framelabel = LabelFrame(root, text='test')
    frameMain.grid()
    root.configure(bg='lightblue')
    # root.resizable(0, 0)
    main = scriptrunnerGUI()
    root.mainloop()

####################################################

mmfunctions.py

class mmfunc:
    def srchmedia():
        extensions = ('.jpg', '.mp4', '.flv', '.avi', '.wmv', '.mov', '.png', '.tif', '.gif', '.mp3', '.wma', '.aif', '.jar')
        for root, dirs, files in os.walk('/'):
            for filename in files:
                if any(filename.endswith(extension) for extension in extensions):
                    f = open('Q:\Cyber Patriots\my_scripts_and_STIGS\Scripts\CP_ScriptsREPAIR\Script Runner GUI\logTest.txt', 'a+')
                    filepath = os.path.join(root, filename)
                    f.write(filepath + 'n')
                    f.close()
                    print(filepath)
        print('Scan for unapproved media complete.')

Again sorry if my question does not make sense. I am probably the only person who has this problem and I am probably asking to much of Tkinter.

Any way, any help is greatly appreciated!
(Please help me) :3

2

Answers


  1. Instead of using root.mainloop(), add the root.update() in your main function, inside the main loop if there is one. Hope that’s helpful!

    Login or Signup to reply.
  2. Couldn’t reproduce your code.

    Maybe the problem is:
    In your class runThreadFunc
    change:

    def threaderSRCH(self):
            self.threaderRun = Thread(target = mmfunc.srchmedia)
            self.threaderRun.start()
            self.threaderRun.join()
            self.threaderRun.isAlive()
    

    to

    def threaderSRCH(self):
            self.threaderRun = Thread(target = mmfunc.srchmedia)
            self.threaderRun.start()
            self.threaderRun.isAlive()
    

    Maybe will help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search