How to execute conditions when pressing button

0

How about, I am new to programming in tkinter and I am creating a graphical interface to generate text with interruptions of a button, my intention is that the code show the alphabet and that the cursor is interrupted in the desired row and starts to advance through columns of the alphabet, and when pressing the button again, the desired letter is selected, this with the intention of generating words that will be shown in the same interface, this would help people who can only move a finger to write , but I'm stuck in the code in doing that by pressing the button change from rows to columns, I would appreciate some guidance on how to do it since the code I have so far I have been doing several post that I have seen, thank you very much.

bot = False

def LA():
    sv.set('''

>A        B       C        D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def LB():
    sv.set('''

 A       >B       C        D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def LC():
    sv.set('''

 A        B      >C        D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def LD():
    sv.set('''

 A        B       C       >D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def N1():
    sv.set('''

 A        B       C        D       >1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def N2():
    sv.set('''

 A        B       C        D        1       >2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')



def LE():

    sv.set('''

 A        B       C        D        1        2

>E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def LI():

    sv.set('''

 A        B       C        D        1        2

 E        F       G        H        3        4

>I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def LO():

    sv.set('''

 A        B       C        D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

>O        P       Q        R        S        T

 U        V       W        X        Y        Z

5        6        7         8        9         0''')

def LU():

    sv.set('''

 A        B       C        D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

>U        V       W        X        Y        Z

5        6        7         8        9         0''')

def N5():

    sv.set('''

 A        B       C        D        1        2

 E        F       G        H        3        4

 I        J       K        L        M        N

 O        P       Q        R        S        T

 U        V       W        X        Y        Z

>5        6        7         8        9         0''')


def buttonClick():
    print("Boton Presionado")
    bot = True
    print(bot) 





from tkinter import *

t = 700

v0 = Tk()

v0.title("Sistema de comunicacion")

v0.geometry("800x480")

sv = StringVar()

et = Label (v0, textvariable = sv, font=("Helvetica", 17))

et.pack()

b = Button(v0, text="Presiona", command=buttonClick)

b.pack()

num = 0

i = 0

while True:



    et.after((num)*t,LA)
    i = i+1

    if (bot == True) :

        et.after((num+i)*t,LB)
        i = i+1

    else :

        et.after((num+i)*t,LE)
        i = i+1

        et.after((num+i)*t,LI)
        i = i+1

        et.after((num+3)*t,LO)
        i = i+1

        et.after((num+4)*t,LU)
        i = i+1

        et.after((num+5)*t,N5)
        i = i+1

    #print(bot)
    num = num + 6


    try:

        v0.update_idletasks()
        v0.update()
    except TclError:
        break
    
asked by Giovazz Bell 13.09.2016 в 02:25
source

1 answer

1

Possibly, tkinter is not the best graphical environment to develop utilities that require good control of events. The after method that is used to launch temporary processes, does not guarantee exact times, so sometimes jumps occur that could mislead and exhaust the user. I advise you to try other GUIs, such as Qt or wx.

Focusing on your code, I have made some improvements to make selections of letters, as you requested. Among other improvements:

  • The creation of the grid is done programmatically. This is simpler to adapt as needed and simpler to locate the selected element.
  • The grid widget is implemented as a class derived from Frame , which improves its reuse.
  • The animation ( self.animate ) uses the after method of the root , which needs to be reactivated once the grid update completes
  • When the button is pressed, the animation should be stopped while the update is being calculated. It is used for it after_cancel , and as a parameter the id of the waiting process that has been stored in variable self._job .

I tried to make it as close as possible to the code of the question. I hope it is understood: ..

import tkinter as tk

GRID = [
    "ABCD12",
    "EFGH34",
    "IJKLMN",
    "OPQRST",
    "UVWXYZ",
    "567890",
]

class Rejilla(tk.Frame):

    def __init__(self, root, t=700):
        super().__init__(root)

        self.root = root
        self.t = t

        self.vertical = True
        self.row = 0
        self.col = 0
        self.nrows = len(GRID)
        self.ncols = len(GRID[0])

        self.var = tk.StringVar()
        self.res = tk.StringVar()

        self.g = tk.Label(root, textvariable = self.var, font=("Courier", 14))
        self.g.pack()

        result = tk.Label(root, textvariable = self.res, font=("Helvetica", 18), bg="yellow", width=30)
        result.pack()

        self.update()
        self.animate()

    def animate(self):
        if self.vertical:
            self.next_row()
        else:
            self.next_col()
        self.update()
        self._job = self.root.after(self.t, self.animate)

    def change_dir(self):
        self.root.after_cancel(self._job)

        if self.vertical:
            self.vertical = False
        else:
            self.vertical = True
            (i,j) = (self.row, self.col)
            (self.row, self.col) = (0,0)

            self.res.set(self.res.get() + GRID[i][j])

        self.update()
        self._job = self.root.after(self.t, self.animate)

    def next_row(self):
        self.row = (self.row+1)%self.nrows
        self.update()

    def next_col(self):
        self.col = (self.col+1)%self.ncols
        self.update()

    def update(self):
        pos = (self.row, self.col)
        self.var.set("\n\n".join(
            "        ".join(
                (">" if (r,c)==pos else " ") + col
                for (c,col) in enumerate(row))
            for (r,row) in enumerate(GRID)   
        ))

if __name__ == "__main__":
    v0 = tk.Tk()
    v0.title("Sistema de comunicacion")
    v0.geometry("800x480")

    r = Rejilla(v0)
    r.pack()

    b = tk.Button(v0, text="Presiona", command=r.change_dir)
    b.pack()

    v0.mainloop()
    
answered by 15.09.2016 в 15:27