Image Viewer error

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Image Viewer error

19 Nov 2018, 14:08

Really breaking myself on this. Below is working code, but comes up with an error that I have no understanding of. Could someone please advise me on how to fix this please? This must be simple, but I just don't know why it's not working (well, the image shows, but the error is always there). "The same variable cannot be used for more than one control"

#Warn
global vPic, Pic

Result := "My Result"
GoSub x

x:
if (Result = "My Result") {
imgpath := "fp001.png"
ShowCard(imgpath)
}
return

ShowCard(imagepath)
{
Gui, +Resize
Gui, Add, Pic, xm vPic
Gui, Show
if imagepath =
return
Gui, Submit, NoHide ; Save the values of the radio buttons.
Height := A_ScreenHeight - 200 ; -1 is "Keep aspect ratio"
Width := -1 ; Minus 28 to allow room for borders and margins inside.
GuiControl, , Pic, *w%width% *h%height% %imagepath% ; Load the image.
Gui, Show, x5 y5 AutoSize, %imagepath% ; Resize the window to match the picture size. ; xCenter puts in middle screen!
; GuiClose:
; return
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Image Viewer error

19 Nov 2018, 14:31

Try and remove the line: Gosub x. untested. I hope that helps.
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Image Viewer error

19 Nov 2018, 16:48

That works thanks, but the reason that I sent the question in that form is that the much longer script that I'm trying to do this in, I've got this part inside the Subroutine. Can you explain to me *why* the GoSub x causes this problem please, I'm a bit stumped as the flow above seems to make sense from what I can see, right?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Image Viewer error

19 Nov 2018, 17:23

Just follow the lines as they get executed, you return from the gosub x, and the next line gets executed, that's why you got the error ( that came from Gui, Add )
If you want to keep the gosub, add the missing return between the gosub and the label to avoid "falling through".
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Image Viewer error

19 Nov 2018, 17:34

ok, I think I see, so the always execute section has to have a 'return' at the end of it. I didn't realise that thanks.

To get to my bigger question then, as this stuff does run in my main script and I've fixed the always execute part as you've said, but the big problem is the same which is that it displays the image perfectly, it pops up on screen (so at the line ShowCard(imgpath) ) but then after about 100 ms it disappears again. Is there something that I need to add to the function to keep the image window showing? Am I doing something (or omitting something that I need) with the Gui, commands that is causing the window to close?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Image Viewer error

19 Nov 2018, 18:52

I can give you some hints, the problem you describe does not happen to me.
Instead, i get some other presumably undesired flashing of an empty GUI, which gets moved towards the upper left corner.

To follow: (1) make breakpoint after first GUI, show ( breakpoint = two lines of code: ListVarsMsgBox )
(2) this will stop the script right there, and you get to look at the Gui, all the global vars, and also local vars, when used inside a function.

I hope that helps.
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Image Viewer error

19 Nov 2018, 22:25

I think the following might be going on, though I don't know how to fix it.
I use the RadioBoxEx function and that calls the Image Viewer. So one Gui is running (the RadioBoxEx), then the other Gui instance runs (the ShowCard(imagepath)). I want to see the RadioBoxEx after I show the image though, so the RadioBoxEx starts again, and that seems to be when the ShowCard dies. Does that seem feasible?
I tried adding some tags in, like Gui, 77:+Resize, but all I managed to do with that was get "Error: The same variable cannot be used for more than one control.", so maybe I'm wrong to think this is the problem area?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Image Viewer error

20 Nov 2018, 04:13

I believe your greatest progress will be to realize that Gui, Add MUST NEVER be called repeatedly. Cause it has a vLabel option.
Why do you need this control to be added? How many do you need?

I have not seen your main script, can you get away with a 5 added pictures? or, better yet, a single picture control, that needs to refresh at times?
I would try to display a single picture, then refresh.

PS: I tried to run the script posted with my own picture, What I see: flashing empty GUI, moved to the UpperLeft, picture NEVER disappears.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Image Viewer error

20 Nov 2018, 04:18

To be clear: your current design does not allow to ever call ShowCard(imagepath) a second time.
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Image Viewer error

20 Nov 2018, 05:16

ok, thanks wolf... I guess it's close to the problem then :). I ripped everything apart (this comes from a 3000 line script) and boiled down the problem as I see it now. The following runs, and you can see the exact problem, the fp001.png flashes open then closes again. Hopefully you can see immediately what I'm doing wrong here and how I can fix the flow (if you want to see the issue, just change the png in line 15 to an existing image). By the way, I would prefer to add formatting to this, but for weeks, with Chrome, I see no option to add AHK formatting to a code block in this forum (maybe it's a setting, dunno, the 'Select code' has AutoIt and tons of others but no AHK)

global vPic, Pic
GoSub ShowResult
return

; ------

ShowResult:
Title := "Show Title"
Prompt := "Click the thing"
List := "Thing|Thing1|Thing2"
WantNumeric := False

Result := RadioBoxEx(Title, Prompt, List, WantNumeric)
if (Result = "Thing") {
imgpath := "C:\0\fp001.png"
ShowCard(imgpath)
}
return

ShowCard(imagepath)
{
Gui, +Resize
Gui, Add, Pic, xm vPic
Gui, Show
if imagepath =
return
Gui, Submit, NoHide ; Save the values of the radio buttons.
Height := A_ScreenHeight - 200 ; -1 is "Keep aspect ratio"
Width := -1 ; Minus 28 to allow room for borders and margins inside.
GuiControl, , Pic, *w%width% *h%height% %imagepath% ; Load the image.
Gui, Show, x5 y5 AutoSize, %imagepath% ; Resize the window to match the picture size. ; xCenter puts in middle screen!
; GuiClose:
; return
}

;-------------------------------------------------------------------------------
RadioBoxEx(Title := "", Prompt := "", List := "", WantNumeric := False) {
;-------------------------------------------------------------------------------
; show a custom input box with Radio buttons
; return the text/index of the selected choice
;
; Title is the title for the GUI
; Prompt is the text to display
; List is a pipe delimited list of choices
; WantNumeric determines if the result is numeric or textual
;
; Note: If the user clicks on the OK button without having made a choice,
; the function returns an empty string or 0.
;---------------------------------------------------------------------------

static Index ; used as a GUI control variable

; create GUI
Gui, RadioBoxEx: New, +LastFound, %Title%
Gui, -MinimizeBox
Gui, Margin, 30, 18
Gui, Add, Text,, %Prompt%
Loop, Parse, List, |
{
options := A_Index = 1 ? "vIndex Section" : ""
if (A_Index != 1) and Mod(A_Index, 10) = 1
options .= " ys"
Gui, Font, cBlue s9 Bold, Tahoma
Gui, Add, Radio, %options%, %A_LoopField%
}
Gui, Add, Button, xm w80 Default, &OK
Gui, Add, Button, x+m wp, &Cancel

; main loop
Gui, Show
WinWaitClose
Return, WantNumeric ? Index : Text


;-----------------------------------
; event handlers
;-----------------------------------
RadioBoxExButtonOK: ; "OK" button, {Enter} pressed
Gui, Submit ; get Index from GUI
GuiControlGet, Text,, Button%Index%, Text
Gui, Destroy
Return

RadioBoxExButtonCancel: ; "Cancel" button
RadioBoxExGuiClose: ; {Alt+F4} pressed, [X] clicked
RadioBoxExGuiEscape: ; {Esc} pressed
Index := 0
Gui, Destroy
Return
}
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Image Viewer error

20 Nov 2018, 05:18

Ideally, I want to keep the RadioBoxEx open at all times, and then select and open images to look at them, then to close the RadioBoxEx instance only when I press Esc or Cancel or Alt-F4 as you set as built-in options.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Image Viewer error

20 Nov 2018, 06:54

Quick fix: to stop flashing of empty GUI, remove the first Gui, Show. I still can not see any images disappearing.. does that still happen?

I guess you would not be interested in quick fixes, (coming from 3000 lines of code), but you might want to re-design your structure.

RadioBox(Ex) is not designed to be kept open. it is meant to interact with the user. Like this: We want an answer, and we want it NOW.
And the Boxes (similar to MsgBox and InputBox) won't allow to carry on any code up until user provides an answer.

To keep a configuration GUI open at all times, you must at least remove "WinWaitClose" from RadioBox(Ex) (untested), very likely you have to make more changes than that. Thinking about ... you maybe can add a callback, so when the user decides to change the config, You can react to it.

I tested this OK: Gui, Add does not get called multiple times any longer.

Code: Select all

global Pic
Gui, Main: New, +Resize
Gui, Add, Pic, xm vPic

GoSub ShowResult
return

; ------

ShowResult:
    Title := "Show Title"
    Prompt := "Click the thing"
    List := "Thing|Thing1|Thing2"
    WantNumeric := False
    Result := RadioBoxEx(Title, Prompt, List, WantNumeric)
    
    if (Result = "Thing") {
        imgpath := "R:\Temp\Rynn.png"
        ShowCard(imgpath)
    }
return

ShowCard(imagepath) {
    Gui, Main: Default
    Height := A_ScreenHeight - 200 ; -1 is "Keep aspect ratio"
    Width := -1 ; Minus 28 to allow room for borders and margins inside.
    GuiControl,, Pic, *w%width% *h%height% %imagepath% ; Load the image.
    Gui, Show, x5 y5 AutoSize, %imagepath% ; Resize the window to match the picture size. ; xCenter puts in middle screen!
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, RussF and 301 guests