How to set focus to the only button?

Hello!
How to set focus to the only button in the application when it starts?

Welcome @Vadim_Y :grin:

It is probably better to provide more details - if you have some sample code that always helps people understand your question.

In your case I believe you want use the focus method on your button once the frame and button have been initialised I’m assuming you are using the The Racket Graphical Interface Toolkit.

You may also want to look at gui-easy as an alternative way to implement it.

Either way - please let us know if you succeed or if you have more troubles?

Best regards

Stephen

https://docs.racket-lang.org/gui/button_.html

https://docs.racket-lang.org/gui/window___.html#(meth._(((lib._mred%2Fmain..rkt)._window~3c~25~3e)._focus))

https://docs.racket-lang.org/gui-easy/index.html

Do you mean this:

#lang racket/gui

(define frame    (new frame% [label "test"] [width 200] [height 200]))
(define callback (λ (this-button event) (eprintf "event: ~a\n" event)))
(define button   (new button% [parent frame] [callback callback] [label "focus!"]))
(send button focus)

(send frame show #true)
1 Like

Thanks for your reply.
I recently started learning GUI.
My application is a simple shell to a Korean-Russian dictionary.
When you start the app, it displays the Korean word. When you click the button
"Next", it displays the translation.
When you click again, the program ends.
The button is implemented as follows:

(make-object button% (~a " Next ") frame (check 1))

Below is the GUI part of the application.

(define frame (new frame%
                   [label "Dictionary"]
                   [width window-w]
                   [height window-h]
                   [alignment '(center center)]))

(new canvas% [parent frame]
             [paint-callback
              (lambda (canvas dc)
                (send dc set-scale scale-1 scale-1)
                (send dc set-text-foreground color-1)
                (send dc draw-text (next-sentence) 10 20))])

(send frame show #t)

(define clicks 2)

(define ((check i) btn evt)
  (new canvas% [parent frame]
             [paint-callback
              (lambda (canvas dc)
                (when (= clicks 0) (exit))
                (set! clicks (sub1 clicks))
                (send dc set-scale scale-2 scale-2)
                (send dc set-text-foreground color-2)
                (send dc draw-text (next-sentence) 10 10))]))
                
(make-object button% (~a " Next ") frame (check 1)) 

The "Next" button fires on the first click.
I want to press only "Enter" key.

Here is a minmal change to your program to get exactly what you asked for.

#lang racket/gui

(define window-w 600)
(define window-h 800)
(define scale-1   10)
(define scale-2   20)
(define color-1 "black")
(define color-2 "green")
(define [next-sentence . x] "next sentence")

(define frame-on%
  (class frame%
    (super-new)
    (define/override (on-subwindow-char a evt)
      (when  (equal? #\return (send evt get-key-code))
        (send button command (new control-event% [event-type 'button]))))))

(define frame
  (new frame-on%
       [label "Dictionary"]
       [width window-w]
       [height window-h]
       [alignment '(center center)]))

(new canvas% [parent frame]
     [paint-callback
      (lambda (canvas dc)
        (send dc set-scale scale-1 scale-1)
        (send dc set-text-foreground color-1)
        (send dc draw-text (next-sentence) 10 20))])

(define clicks 2)

(define ((check i) btn evt)
  (new canvas% [parent frame]
       [paint-callback
        (lambda (canvas dc)
          (when (= clicks 0)


            (exit))
          (set! clicks (sub1 clicks))
          (send dc set-scale scale-2 scale-2)
          (send dc set-text-foreground color-2)
          (send dc draw-text (next-sentence) 10 10))]))

(define button (new button% [label (~a " Next ")] [parent frame] [callback (check 1)]))

(send button focus)

(send frame show #t)
1 Like

EmEf, your answer helped me solve the problem.
Thank you very much!

1 Like