Apr-17-2021, 11:04 PM
(This post was last modified: Apr-17-2021, 11:19 PM by knoxvilles_joker.
Edit Reason: forgot variable part of solution
)
(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 onon_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 thetextvariable
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")