0

Here's my problem, when keyboard focus is on KeyboardEntry, KeysPressed() should happen, when focus leaves, toggle() should happen. KeysPressed has a while loop such as:

while Flag:
       #Whatever

toggle just toggles the flag

  Flag= not Flag

The idea is when focus leaves KeyComboEntry, KeysPressed() stops running This does not happen, at all matter of fact, toggle is not called my

 # Program by Fares Al Ghazy started 20/5/2017
 # Python script to assign key combinations to bash commands, should run in the background at startup
  # this program is meant to release bash code, it is obviously non-system agnostic and only works linux systems that use BASH
  # is one file which only creates the GUI, another file is needed to use the info taken by this program

FileName  = 'BinderData.txt'

import tkinter as tk
from ComboDetect import ComboDetector
from _thread import start_new_thread
# Create a class to get pressed keys and print them
KeyManager = ComboDetector()


# Class that creates GUI and takes info to save in file

class MainFrame(tk.Tk):
    # variable to store pressed keys
    KeyCombination = ""
    testcounter = 1
    Flag = True
    #function to toggle Flag
    def Toggle(self):
        print("toggle")
        self.Flag = not self.Flag
    # function to write to file
    def SaveFunction(self, e1, e2, FileName):
        file = open(FileName, "a")
        combo = e1.get() + '\n'
        performed = e2.get() + '\n'
        file.write(combo)
        file.write(performed)
        file.close()

    def KeysPressed(self, Entry, KeyCombination):
        Entry.config(state="normal")
       #Entry.insert(tk.END, "Test")
        while self.Flag:
            print("test "+str(self.testcounter))
            self.testcounter = self.testcounter + 1
            KeyCombination = str(KeyManager.getpressedkeys())
            Entry.delete(0, tk.END)
            Entry.insert(tk.END, KeyCombination)


    # constructor

    def __init__(self, FileName, **kwargs):
        tk.Tk.__init__(self, **kwargs)
        # create GUI to take in key combinations and bash codes, then save them in file
        root = self  # create new window
        root.wm_title("Key binder")  # set title
        #  create labels and text boxes
        KeyComboLabel = tk.Label(root, text="Key combination = ")
        KeyComboEntry = tk.Entry(root)

        # Bind function to entry

        KeyComboEntry.bind('<FocusIn>', lambda e: start_new_thread(self.KeysPressed, (KeyComboEntry, self.KeyCombination)))
        KeyComboEntry.bind('<FocusOut>', lambda f:self.toggle, ())
        ActionLabel = tk.Label(root, text="Command to be executed = ")
        ActionEntry = tk.Entry(root)
        # place widgets in positions
        KeyComboLabel.grid(row=0, column=0, sticky=tk.E)
        ActionLabel.grid(row=1, column=0, sticky=tk.E)

        KeyComboEntry.grid(row=0, column=1)
        ActionEntry.grid(row=1, column=1)
        # create save button
        SaveButton = tk.Button(root, text="save",
                               command=lambda: self.SaveFunction(KeyComboEntry, ActionEntry, FileName))
        SaveButton.grid(row=2, column=2, sticky=tk.E)


app = MainFrame(FileName)
app.mainloop()     
  • There is no reason to be using threading for something which is literally going to take milliseconds to execute (`self.toggle`). – Pythonista Jun 13 '17 at 23:18
  • true, forgot to change that back, thanks. – Fares Al Ghazy Jun 13 '17 at 23:21
  • 1
    `ModuleNotFoundError: No module named 'ComboDetect'`. Please post a [mcve] that focuses on your problem. – PM 2Ring Jun 13 '17 at 23:53
  • This is a follow-on question to https://stackoverflow.com/questions/44486298/tkinter-bind-does-not-call-funtion – PM 2Ring Jun 13 '17 at 23:53
  • 1
    I strongly encourage you to _not_ use lambda unless you really need to. It adds complexity, and makes your code harder to understand and harder to debug. Also, you can't use threads in this way. All tkinter commands need to be called in the same threaf. – Bryan Oakley Jun 14 '17 at 00:29
  • @PM2Ring ComboDetect isn't causing the problem here, toggle is not being called, regardless of combodetect – Fares Al Ghazy Jun 14 '17 at 06:22

0 Answers0