Seite 1 von 1

Checkboxen realtime abfragen

Verfasst: Montag 7. Februar 2005, 14:55
von Iopodx@Gast
Ja, wie der Name schon sagt würde ich gerne Checkboxen alle Zehntelsekunde abfragen ob etwas geändert wurde.

Jemand ne idee wie ich das machen könnte? Hab schon bissl mit Threads etc. probiert... aber entweder gehts nicht, oder das ´Frame wird garnet erst aufgebaut...

Danke schonmal im vorraus.

Verfasst: Montag 7. Februar 2005, 15:13
von mawe
Hi!

Irgendwie steh ich aufm Schlauch, aber was sind Checkboxen? Oder meinst Du Checkbuttons?

UPDATE:
Wenn Du Checkbuttons meinst, warum willst Du die abfragen? Wenn sich etwas ändert, sollen dir das die Checkbuttons selbst sagen. Dafür haben sie ja die Funktion command:

Code: Alles auswählen

from Tkinter import *

def show_it():
    print s.get()

root = Tk()
s = StringVar()
Checkbutton(text="Check",onvalue="on",offvalue="off",variable=s,command=show_it).pack()
root.mainloop()

Gruß, mawe

Verfasst: Montag 7. Februar 2005, 15:47
von Iopodx@Gast
Erstmal Danke für die schnelle Antwort

Ja, die meine ich ;). Sorry.

Also die Sache ist so, ich habe 4Checkbuttons, und je nach reihenfolge der "On/Off" Werte, also 0/1, wird ein Wert ausgegeben. Bis jetzt muss ich nach jeder Änderung der Checkbuttons wieder auf einen normalen Button klicken der dann einen CMD ausführt welcher dann die 4Checkbutton Werte aus gibt. Also 4 mal 0 oder 1.

Ach herrje ist das kompliziert. Ich hoffe du hast das ganze verstanden... Ich poste im Anhang mal den Code damit du mich dann hoffentlich verstehst...

Code: Alles auswählen

from random import randint
from Tkinter import *
from thread import start_new_thread
from time import sleep

dict={
    '[0, 0, 0, 0]':'0',
    '[1, 0, 0, 0]':'1',
    '[0, 1, 0, 0]':'2',
    '[0, 0, 1, 0]':'3',
    '[0, 0, 0, 1]':'4',
    '[1, 1, 0, 0]':'5',
    '[1, 0, 1, 0]':'6',
    '[1, 0, 0, 1]':'7',
    '[1, 1, 1, 0]':'8',
    '[1, 1, 0, 1]':'9',
    '[1, 1, 1, 1]':':',
    '[0, 1, 0, 1]':'=',
    '[1, 0, 1, 1]':' ',
    '[0, 1, 1, 0]':'.',
    '[0, 1, 1, 1]':',',
    '[0, 0, 1, 1]':'E'
    }
redict={
    '0':'[0, 0, 0, 0]',
    '1':'[1, 0, 0, 0]',
    '2':'[0, 1, 0, 0]',
    '3':'[0, 0, 1, 0]',
    '4':'[0, 0, 0, 1]',
    '5':'[1, 1, 0, 0]',
    '6':'[1, 0, 1, 0]',
    '7':'[1, 0, 0, 1]',
    '8':'[1, 1, 1, 0]',
    '9':'[1, 1, 0, 1]',
    ':':'[1, 1, 1, 1]',
    '=':'[0, 1, 0, 1]',
    ' ':'[1, 0, 1, 1]',
    '.':'[0, 1, 1, 0]',
    ',':'[0, 1, 1, 1]',
    'E':'[0, 0, 1, 1]'
    }
def update(A, B, C, D, resulta):
    while 1:
        go(A, B, C, D, resulta)
        sleep(0.1)
def go(A, B, C, D, resulta, *key):
    if A=='':
        A=0
    if B=='':
        B=0
    if C=='':
        C=0
    if D=='':
        D=0
    A, B, C, D=str(A), str(B), str(C), str(D)
    a=dict['['+A+', '+B+', '+C+', '+D+']']
    try: resulta.destroy()
    except: pass
    result=Label(root, text='  ', font="TimesNewRoman -50")
    result.place(x=30, y=100)
    result=Label(root, text=a, font="TimesNewRoman -50")
    result.place(x=30, y=100)
    
def set_it(A, B, C, D, Ac, Bc, Cc, Dc, resulta):
    if (A==1):
        Ac.select()
    if (B==1):
        Bc.select()
    if (C==1):
        Cc.select()
    if (D==1):
        Dc.select()
    if (A==0):
        Ac.deselect()
    if (B==0):
        Bc.deselect()
    if (C==0):
        Cc.deselect()
    if (D==0):
        Dc.deselect()
    go(A, B, C, D, resulta)
def convert(result, Ac, Bc, Cc, Dc, resulta):
    if result==():
        result=('0')
    list=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', '=', ' ', '.', ',', 'E']
    A=int(redict[list[int(result[0])]][1])
    B=int(redict[list[int(result[0])]][4])
    C=int(redict[list[int(result[0])]][7])
    D=int(redict[list[int(result[0])]][10])
    set_it(A, B, C, D, Ac, Bc, Cc, Dc, resulta)
root=Tk()
root.minsize(170, 240)
root.maxsize(170, 240)
result=Label(root)
Label(root, text='A').place(x=5, y=0)
A, B, C, D=StringVar(), StringVar(), StringVar(), StringVar()
Ac=Checkbutton(root, variable=A)
Ac.place(x=0, y=15)
Label(root, text='B').place(x=25, y=0)
Bc=Checkbutton(root, variable=B)
Bc.place(x=20, y=15)
Label(root, text='C').place(x=45, y=0)
Cc=Checkbutton(root, variable=C)
Cc.place(x=40, y=15)
Label(root, text='D').place(x=65, y=0)
Dc=Checkbutton(root, variable=D)
Dc.place(x=60, y=15)
Button(root, text="Update!", command=lambda:go(A.get(), B.get(), C.get(), D.get(), result)).place(x=40, y=70)
root.bind('<Return>', lambda x:go(A.get(), B.get(), C.get(), D.get(), result))
box=Listbox(root, width=4, height=16)
box.place(x=100, y=0)
for i in range(0, 10):
    box.insert(END, str(i))
box.insert(END, ':')
box.insert(END, '=')
box.insert(END, ' ')
box.insert(END, '.')
box.insert(END, ',')
box.insert(END, 'E')
box.bind('<Button-1>', lambda x:convert(box.curselection(), Ac, Bc, Cc, Dc, result))
#start_new_thread(update, (A.get(), B.get(), C.get(), D.get(), result))
root.mainloop()
Es soll Quasi der Klick auf den Button alle 0.1 Sekunden ausgeführt werden!

Verfasst: Montag 7. Februar 2005, 15:57
von mawe
Hi!

Wirklich kompliziert! :)

Ich bleibe aber bei meinem Vorschlag mit command. Mach einfach eine Funktion get_them und nimm sie bei jedem Checkbutton als command:

Code: Alles auswählen

def get_them():
    go(A.get(), B.get(), C.get(), D.get(), result)

root = Tk()
...
Ac = Checkbutton(root, variable=A, command=get_them)
...
Jedesmal wenn jetzt ein Checkbutton gedrückt wird, wird go aufgerufen, genauso wie wenn ich den Button drücke.
Geht das auch, oder musst Du die Werte alle 1/10 sec abfragen?

Gruß, mawe

Verfasst: Montag 7. Februar 2005, 16:14
von Iopodx@Gast
Ach, super. Genau so wollt ich das:)

Fein!

Sorry, hatte deinen ersten Post zu flüchtig gelesen :oops:

Danke!