Python Forum
update text variable on label with keypress
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
update text variable on label with keypress
#3
(Apr-17-2021, 08:40 PM)Yoriz Wrote:
(Apr-17-2021, 07:03 PM)knoxvilles_joker Wrote: I am trying to get a gui to update with four different possible letters to display in the left and right top corners of the window (a, b, c, d). I am having issues finding a good example of something similar already done.
This doesn't clearly describe what you intend to do, what decides which of the four choices (a, b, c, d) is made and what decides what goes to which corner?

(Apr-17-2021, 07:03 PM)knoxvilles_joker Wrote: I am just trying to wrap my brain around how to get the keypress to update a text string variable in a label in tkinter. I tried tk.Label, I could try ttk.Label.
you have a text string variable in the code already
self.stat = tk.StringVar()
that is updated on on_keypress_f
using
self.stat.set(stat_value)
which is updating a button
crit = tk.Button(self,justify=tk.CENTER,textvariable=self.stat, bg='black', fg='yellow', highlightbackground='yellow', highlightcolor='black',activebackground='yellow')
do the same thing for a label using the textvariable parameter


Durr, I am a dunce. Were that code piece a snake I would be dead on arrival at the hospital.

Your points are valid. I was not explicitly clear:
If "A" is pressed, A displays in the left and right upper corners
If "B" is pressed, B displays in the left and right upper corners
If "C" is pressed, C displays in the left and right upper corners
If "D" is pressed, D displays in the left and right upper corners

That was so easy I overlooked the darned solution.
Declare the global text string:
GUN = ""
Declare a local variable text string:
        self.gun = tk.StringVar()
        self.gun.set(GUN)
Set these binds:
        self.bind("<KeyPress-a>", self.on_keypress_a)
        self.bind("<KeyPress-b>", self.on_keypress_b)
        self.bind("<KeyPress-c>", self.on_keypress_c)
        self.bind("<KeyPress-d>", self.on_keypress_d)
to these functions:
    def on_keypress_a(self, evet):
        which_gun = self.gun.get()
        which_gun = "A"
        self.gun.set(which_gun)

    def on_keypress_b(self, evet):
    def on_keypress_a(self, evet):
        which_gun = self.gun.get()
        which_gun = "B"
        self.gun.set(which_gun)

    def on_keypress_c(self, evet):
    def on_keypress_a(self, evet):
        which_gun = self.gun.get()
        which_gun = "C"
        self.gun.set(which_gun)

    def on_keypress_d(self, evet):
    def on_keypress_a(self, evet):
        which_gun = self.gun.get()
        which_gun = "D"
        self.gun.set(which_gun)
Which updates these labels:
        # displays gun id on top left and right of screen
        gun_1 = tk.Label(self, borderwidth=7,bg='black',fg='yellow',disabledforeground='yellow',state=tk.DISABLED,textvariable=self.gun)
        gun_1.grid(row=0,column=0,rowspan=2,sticky="NS")
        gun_2 = tk.Button(self, borderwidth=7,bg='black',fg='yellow',disabledforeground='yellow',state=tk.DISABLED,textvariable=self.gun)
        gun_2.grid(row=0,column=5,rowspan=2,sticky="NS")
Reply


Messages In This Thread
RE: update text variable on label with keypress - by knoxvilles_joker - Apr-17-2021, 11:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 976 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,603 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 5,042 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,276 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [Tkinter] Make my button text update? Skata100 1 2,062 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 7,912 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  Updating button text based upon different variable values knoxvilles_joker 0 2,255 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,690 Mar-10-2021, 04:21 PM
Last Post: Sir
  How to read text in kivy textinput or Label jadel440 1 5,317 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Kivy] Kivy text label won't shows up! AVD_01 1 2,965 Jun-21-2020, 04:01 PM
Last Post: AVD_01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020