TrayIcon picture not updating

Post

Posted
Rating:
#1 (In Topic #564)
Trainee
Hello,

I'm writing a form with a TrayIcon that should change according periodically using a timer:

Code

Public Sub Form_Open()
  StatusTimer.Delay = 1
  TrayIcon1.Visible = True
End

Public Sub StatusTimer_Timer()
  UpdateTrayIcon
  StatusTimer.Delay = Globals.StatusTimerDelay
End

Private Sub UpdateTrayIcon()
  Dim CnxStatus As String
  CnxStatus = GetCnxStatus()
  If InStr(CnxStatus, "Connected") > 0 Then
    TrayIcon1.Tooltip = "Connected"
    TrayIcon1.Picture = Picture["icon1.png"]
  Else If InStr(CnxStatus, "Not connected") > 0 Then
    TrayIcon1.Tooltip = "Not connected"
    TrayIcon1.Picture = Picture["icon2.png"]
  Endif
  Print TrayIcon1.Tooltip
End

The first time the timer is invoked the icon is displayed correctly, but after that the icon picture remain the same, even when the returned status changed (the tooltip is updated correctly). Please advise.

TIA
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

ForeverNoob said

…even when the returned status changed (the tooltip is updated correctly). Please advise.

TIA

Hi TIA and welcome to Gambas One.

I can't immediately see a problem with your code, but I suggest you put a break point at the line starting: "If "

Then run the code and when it stops use F8 to step it on one line at a time while checking variables.

If I get a chance later today I may try to reproduce your problem.

A couple of other tips; paste code on these pages using the gb button, as this preserves Gambas formatting…

Code (gambas)

  1. [code]
  2. Public Sub Form_Open()
  3.   StatusTimer.Delay = 1
  4.   TrayIcon1.Visible = True
  5.  
  6. Public Sub StatusTimer_Timer()
  7.   UpdateTrayIcon
  8.   StatusTimer.Delay = Globals.StatusTimerDelay
  9.  
  10. Private Sub UpdateTrayIcon()
  11.   Dim CnxStatus As String
  12.   CnxStatus = GetCnxStatus()
  13.   If InStr(CnxStatus, "Connected") > 0 Then
  14.     TrayIcon1.Tooltip = "Connected"
  15.     TrayIcon1.Picture = Picture["icon1.png"]
  16.   Else If InStr(CnxStatus, "Not connected") > 0 Then
  17.     TrayIcon1.Tooltip = "Not connected"
  18.     TrayIcon1.Picture = Picture["icon2.png"]
  19.   Print TrayIcon1.Tooltip
  20. [/code]


…I also suggest that you enable line numbers in the editor, as they can be very handy!

I hope this helps.

Steve
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’
OK, I've knocked up a simple program and single-stepped my way through it.

Image

(Click to enlarge)


The only thing I notice is that while the TrayIcon object is updated as expected, the tray icon only gets updated on the screen when the code exits my timer, which calls the Sub, which changes the TrayIcon icon/picture. Adding Wait statements didn't help (no real reason why they should, its not the form that needs to update its the system panel...but worth a try).

Can you humour me and just temporarily increase your timer delay from 1 second to 10 seconds and retest?
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'.

Run this code in a Graphical program with 'gb.gui.trayicon' selected.

Code (gambas)

  1. bSwitch As Boolean
  2. Timer1 As Timer
  3. TrayIcon1 As Trayicon
  4. Label1 As Label
  5.  
  6. Public Sub Form_Open()
  7.  
  8.   With Me
  9.     .Height = 300
  10.     .Width = 650
  11.     .Arrangement = Arrange.Vertical
  12.     .Padding = 5
  13.  
  14.   With Label1 = New Label(Me) As "Label1"
  15.     .Expand = True
  16.     .Height = 28
  17.     .Font.Size = 60
  18.     .Font.Bold = True
  19.     .Alignment = Align.Center
  20.  
  21.   With TrayIcon1 = New TrayIcon As "TrayIcon1"
  22.     TrayIcon1.Tooltip = "Connected"
  23.  
  24.   With Timer1 = New Timer As "Timer1"
  25.     .Delay = 3000
  26.     .Trigger
  27.     .Start
  28.  
  29.  
  30. Public Sub Timer1_Timer()
  31.  
  32.   TrayIcon1.Hide                                    'This is an important part
  33.  
  34.   If bSwitch = False Then
  35.     TrayIcon1.Tooltip = "Connected"
  36.     Label1.Text = "Connected"
  37.   Else
  38.     TrayIcon1.Tooltip = "Not Connected"
  39.     Label1.Text = "Not connected"
  40.   End If
  41.  
  42.   TrayIcon1.Show                                    'This is an important part
  43.   bSwitch = Not bSwitch
  44.  
  45.  
  46. Public Sub Form_Close()
  47.  
  48.   TrayIcon1.Delete                                  'This stops the program from continuing to run
  49.  
  50.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'….

By 'comment' do you mean ToolTip Charlie?

If I remove the breakpoints and just run my code with TimerDelay of 5 seconds its works fine, including the ToolTips.
(Actually, If I run with TimerDelay set to 1ms is still runs fine, but no tool tips.)

I didn't find .Show/.Hide/.Delete of any help with Icon/Picture.

I wonder if this problem is related to the Linux desktop in use, because there is some old chat in TrayIcon Help about DBus and Ubuntu implementations…


Image

(Click to enlarge)



…or there could still be a problem with TIA's code. Maybe you could upload the project so we can take a look?
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee

stevedee said

OK, I've knocked up a simple program and single-stepped my way through it.

TrayIcon.png

The only thing I notice is that while the TrayIcon object is updated as expected, the tray icon only gets updated on the screen when the code exits my timer, which calls the Sub, which changes the TrayIcon icon/picture. Adding Wait statements didn't help (no real reason why they should, its not the form that needs to update its the system panel...but worth a try).

Can you humour me and just temporarily increase your timer delay from 1 second to 10 seconds and retest?

Hi stevedee,

Thanks for answering. I tried increasing the initial timer delay, but that didn't work. In fact, over the value of 50(ms) or so the TrayIcon didn't updated even on the first iteration and just displayed the default gambas icon.
Online now: No Back to the top

Post

Posted
Rating:
#7
Trainee

cogier said

I have run into one part of this problem which is the 'Comment' not changing. This can be solved by 'Hiding' then 'Showing' the icon which will change the 'Comment'. However, I can't get this to work with the 'Picture'.

Run this code in a Graphical program with 'gb.gui.trayicon' selected.

Code (gambas)

  1. bSwitch As Boolean
  2. Timer1 As Timer
  3. TrayIcon1 As Trayicon
  4. Label1 As Label
  5.  
  6. Public Sub Form_Open()
  7.  
  8.   With Me
  9.     .Height = 300
  10.     .Width = 650
  11.     .Arrangement = Arrange.Vertical
  12.     .Padding = 5
  13.  
  14.   With Label1 = New Label(Me) As "Label1"
  15.     .Expand = True
  16.     .Height = 28
  17.     .Font.Size = 60
  18.     .Font.Bold = True
  19.     .Alignment = Align.Center
  20.  
  21.   With TrayIcon1 = New TrayIcon As "TrayIcon1"
  22.     TrayIcon1.Tooltip = "Connected"
  23.  
  24.   With Timer1 = New Timer As "Timer1"
  25.     .Delay = 3000
  26.     .Trigger
  27.     .Start
  28.  
  29.  
  30. Public Sub Timer1_Timer()
  31.  
  32.   TrayIcon1.Hide                                    'This is an important part
  33.  
  34.   If bSwitch = False Then
  35.     TrayIcon1.Tooltip = "Connected"
  36.     Label1.Text = "Connected"
  37.   Else
  38.     TrayIcon1.Tooltip = "Not Connected"
  39.     Label1.Text = "Not connected"
  40.   End If
  41.  
  42.   TrayIcon1.Show                                    'This is an important part
  43.   bSwitch = Not bSwitch
  44.  
  45.  
  46. Public Sub Form_Close()
  47.  
  48.   TrayIcon1.Delete                                  'This stops the program from continuing to run
  49.  
  50.  

Hi cogier,

Thanks for answering. I've copied the above code to a new form and eidted the relevant part to look like this:

Code (gambas)

  1.   If bSwitch = False Then
  2.     TrayIcon1.Tooltip = "Connected"
  3.     TrayIcon1.Picture = Picture["icon1.png"]
  4.     Label1.Text = "Connected"
  5.   Else
  6.     TrayIcon1.Tooltip = "Not Connected"
  7.     TrayIcon1.Picture = Picture["icon2.png"]
  8.     Label1.Text = "Not connected"
  9.   End If
  10.  

The label is periodically updated, but not the icon.
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 OK, this is what I'm going to do: I'll create 2 TrayIcons, each with it's own icon, and just hide or show the relevant one as needed. No use fighting what looks like a bug somewhere.

Thank you all for the time and effort.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
stevedee is in the usergroup ‘Regular’

ForeverNoob said

…The label is periodically updated, but not the icon.

Could you post the details of your system?
including:-
  • Linux Distro
  • Linux kernel
  • Gambas version

…and maybe go to menu Project > Properties and post a screen-shot like this;


Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee

stevedee said

ForeverNoob said

…The label is periodically updated, but not the icon.

Could you post the details of your system?
including:-
  • Linux Distro
  • Linux kernel
  • Gambas version

…and maybe go to menu Project > Properties and post a screen-shot like this;


TrayIconProperties.png

Mint 20.1 cinnamon
Kernel 5.4.0-62-generic
Gambas 3.14.3
Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Guru
cogier is in the usergroup ‘Guru’

ForeverNoob said


Hi cogier,

Thanks for answering. I've copied the above code to a new form and edited the relevant part to look like this:

Code (gambas)

  1.   If bSwitch = False Then
  2.     TrayIcon1.Tooltip = "Connected"
  3.     TrayIcon1.Picture = Picture["icon1.png"]
  4.     Label1.Text = "Connected"
  5.   Else
  6.     TrayIcon1.Tooltip = "Not Connected"
  7.     TrayIcon1.Picture = Picture["icon2.png"]
  8.     Label1.Text = "Not connected"
  9.   End If
  10.  

The label is periodically updated, but not the icon.

You need the Hide and Show in your code, or it won't work properly. I did mark it as 'Important'

Code (gambas)

  1. TrayIcon1.Hide                                    'This is an important part

Code (gambas)

  1. TrayIcon1.Show                                  'This is an important part
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Guru
cogier is in the usergroup ‘Guru’

ForeverNoob said

OK, this is what I'm going to do: I'll create 2 TrayIcons, each with it's own icon, and just hide or show the relevant one as needed. No use fighting what looks like a bug somewhere.

Thank you all for the time and effort.

You're a genius, and it works. Here my modified code.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. bSwitch As Boolean
  4. Timer1 As Timer
  5. TrayIcon1 As Trayicon
  6. TrayIcon2 As Trayicon
  7. Label1 As Label
  8.  
  9. Public Sub Form_Open()
  10.  
  11.   With Me
  12.     .Height = 300
  13.     .Width = 650
  14.     .Arrangement = Arrange.Vertical
  15.     .Padding = 5
  16.  
  17.   With Label1 = New Label(Me) As "Label1"
  18.     .Expand = True
  19.     .Height = 28
  20.     .Font.Size = 60
  21.     .Font.Bold = True
  22.     .Alignment = Align.Center
  23.  
  24.   With TrayIcon1 = New TrayIcon As "TrayIcon1"
  25.     .Tooltip = "Connected"
  26.     .Picture = Picture["icon:/22/connect"]
  27.  
  28.   With TrayIcon2 = New TrayIcon As "TrayIcon2"
  29.     .Tooltip = "Not Connected"
  30.     .Picture = Picture["icon:/22/close"]
  31.  
  32.   With Timer1 = New Timer As "Timer1"
  33.     .Delay = 3000
  34.     .Trigger
  35.     .Start
  36.  
  37.  
  38. Public Sub Timer1_Timer()
  39.  
  40.   If bSwitch = False Then
  41.     TrayIcon1.Show
  42.     TrayIcon2.Hide
  43.     TrayIcon1.Tooltip = "Connected"
  44.     Label1.Text = "Connected"
  45.   Else
  46.     TrayIcon1.Hide
  47.     TrayIcon2.Show
  48.     TrayIcon1.Tooltip = "Not connected"
  49.     Label1.Text = "Not connected"
  50.   End If
  51.  
  52.   bSwitch = Not bSwitch
  53.  
  54.  
  55. Public Sub Form_Close()
  56.  
  57.   TrayIcon1.Delete                                  'This stops the program from continuing to run
  58.  
Online now: No Back to the top

Post

Posted
Rating:
#13
Trainee

cogier said

ForeverNoob said

OK, this is what I'm going to do: I'll create 2 TrayIcons, each with it's own icon, and just hide or show the relevant one as needed. No use fighting what looks like a bug somewhere.

Thank you all for the time and effort.

You're a genius, and it works. Here's my modified code.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. bSwitch As Boolean
  4. Timer1 As Timer
  5. TrayIcon1 As Trayicon
  6. TrayIcon2 As Trayicon
  7. Label1 As Label
  8.  
  9. Public Sub Form_Open()
  10.  
  11.   With Me
  12.     .Height = 300
  13.     .Width = 650
  14.     .Arrangement = Arrange.Vertical
  15.     .Padding = 5
  16.  
  17.   With Label1 = New Label(Me) As "Label1"
  18.     .Expand = True
  19.     .Height = 28
  20.     .Font.Size = 60
  21.     .Font.Bold = True
  22.     .Alignment = Align.Center
  23.  
  24.   With TrayIcon1 = New TrayIcon As "TrayIcon1"
  25.     .Tooltip = "Connected"
  26.     .Picture = Picture["icon:/22/connect"]
  27.  
  28.   With TrayIcon2 = New TrayIcon As "TrayIcon2"
  29.     .Tooltip = "Not Connected"
  30.     .Picture = Picture["icon:/22/close"]
  31.  
  32.   With Timer1 = New Timer As "Timer1"
  33.     .Delay = 3000
  34.     .Trigger
  35.     .Start
  36.  
  37.  
  38. Public Sub Timer1_Timer()
  39.  
  40.   If bSwitch = False Then
  41.     TrayIcon1.Show
  42.     TrayIcon2.Hide
  43.     TrayIcon1.Tooltip = "Connected"
  44.     Label1.Text = "Connected"
  45.   Else
  46.     TrayIcon1.Hide
  47.     TrayIcon2.Show
  48.     TrayIcon1.Tooltip = "Not connected"
  49.     Label1.Text = "Not connected"
  50.   End If
  51.  
  52.   bSwitch = Not bSwitch
  53.  
  54.  
  55. Public Sub Form_Close()
  56.  
  57.   TrayIcon1.Delete                                  'This stops the program from continuing to run
  58.  

 :D Thanks a lot!
Online now: No Back to the top
1 guest and 0 members have just viewed this.