2017-06-11 1 views
-2

Ich versuche, den Text in einem tkinter Eintrag Widget zu ändern, um die vom Benutzer eingegebene Tastenkombination zu sein (Beispiel: ShiftL + ShiftR), das Python-Programm läuft gut, aber Eintrag nicht geändert , warum und wie kann ich es reparieren? Meine GUI:Tkinter Bindung ruft keine Funktion auf

# 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 
# Since this program is meant to release bash code, it is obviously non-system agnostic and only works linux systems that use BASH 
# This 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 

# 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 = "" 

    def KeysPressed(self, Entry, KeyCombination): 
     KeyCombination = KeyManager.getpressedkeys() 
     Entry.delete(0, tk.END) 
     Entry.insert(0, 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>',self.KeysPressed(KeyComboEntry, self.KeyCombination)) 

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

und ComboDetect:

#this program was created by LadonAl (Alaa Youssef) in 25.May.17 
    #it detects a combination of pressed key and stores them in a list and prints the list when at least 
    # one of the keys is released 

import time 
import pyxhook 

class ComboDetector(object): 
    def getpressedkeys(self): 
     return self.combo 

Edit: Ich habe die keyspressed Funktion geändert, es zu testen

def KeysPressed(self, Entry, KeyCombination): 
     Entry.config(state="normal") 
     Entry.insert(tk.END, "Test") 
     print("test") 
     KeyCombination = KeyManager.getpressedkeys() 
     Entry.delete(0, tk.END) 
     Entry.insert(tk.END, KeyCombination) 

Dies ist, was ich bemerkt habe: Wenn die Modul wird ausgeführt, "Test" wird auf Konsole gedruckt, nichts anderes passiert. Wenn ich versuche, außerhalb des Eintrags Widget klicken und wieder hinein klicken (Ausfahrt Fokus und neu eingeben), passiert nichts

+0

Versuchen Entry.insert (tk.END, KeyCombination) statt Entry.insert (0, KeyCombination) – RainingComputers

+0

@RainingComputers hat nicht funktioniert:/ –

+0

Sind Sie sicher, dass Sie erwartet KeyCombination bekommen? Versuchen Sie, es direkt nach der Zuweisung zu drucken. – Lafexlos

Antwort

0

Das Problem ist, dass, wenn Sie zu KeyComboEntry zu binden versuchen, die Prozedur ruft KeysPressed Anstatt bind die Methode für KeyComboEntry zu übergeben, können Sie dies beheben, indem Sie KeyComboEntry.bind("<Key>", self.KeysPressed, KeyComboEntry, self.KeyCombination) verwenden. Bind wird dann KeysPressed mit den Argumenten KeyComboEntry und self.KeyCombination aufrufen. Die andere Alternative ist die Verwendung einer lambda-Funktion, wie Sie sie für SaveButton verwendet haben, wobei bind als Ereignis übergeben wird.

+0

Arbeitete, danke :) –

Verwandte Themen