lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.78k stars 886 forks source link

How to close the window when the button is clicked #795

Closed xcd0 closed 2 years ago

xcd0 commented 2 years ago

I would like to know how to close the window normally when the button is clicked. My source code below ends with an exception.

package main

import (
    . "github.com/lxn/walk/declarative"
)

func main() {
    Run()
    // something
    Run()
}

func Run() {
    var w MainWindow
    w = MainWindow{
        Title:  "test",
        Size:   Size{400, 200},
        Layout: VBox{},
        Children: []Widget{
            PushButton{
                Text: "Exit",
                OnClicked: func() {
                    (**w.AssignTo).Close()
                },
            },
        },
    }
    w.Run()
}

panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0x64de7f]

goroutine 1 [running, locked to thread]: main.Run.func1() example.com/test/main.go:24 +0x1f github.com/lxn/walk.(EventPublisher).Publish(0xc00004fb30) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/event.go:71 +0xf0 github.com/lxn/walk.(Button).raiseClicked(...) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/button.go:195 github.com/lxn/walk.(Button).WndProc(0xc00004f800, 0xc00011a5d0, 0x111, 0x6, 0x5c10f4) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/button.go:210 +0x7f github.com/lxn/walk.(PushButton).WndProc(0xc00004f800, 0x6, 0x111, 0x0, 0xc00004f000) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/pushbutton.go:115 +0x16b github.com/lxn/walk.(ContainerBase).WndProc(0xc00006f180, 0x0, 0x111, 0x6, 0x5c10f4) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/container.go:345 +0x46d github.com/lxn/walk.defaultWndProc(0x0, 0x111, 0xc000114058, 0xc000113d58) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:2159 +0xb9 syscall.Syscall6(0x7ffc7c0de3f0, 0x5, 0x7ffc4c4718f0, 0x5c10f4, 0x202, 0x0, 0x300db, 0x0) runtime/syscall_windows.go:493 +0xfa github.com/lxn/win.CallWindowProc(0xc00004f800, 0xc00004f000, 0x202, 0xc000000400, 0x0) github.com/lxn/win@v0.0.0-20210218163916-a377121e959e/user32.go:2138 +0x86 github.com/lxn/walk.(WindowBase).WndProc(0xc00004f800, 0x0, 0x202, 0x0, 0x300db) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:2511 +0xc68 github.com/lxn/walk.(Button).WndProc(0xc00004f800, 0xc0001145d0, 0x202, 0x0, 0x300db) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/button.go:218 +0x117 github.com/lxn/walk.(PushButton).WndProc(0xc00004f800, 0xc0001146b0, 0x202, 0xc000114938, 0xc000114638) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/pushbutton.go:115 +0x16b github.com/lxn/walk.defaultWndProc(0x0, 0x202, 0xc0001146b0, 0x0) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:2159 +0xb9 syscall.Syscall(0x7ffc7c0dbd60, 0x2, 0x3212b4, 0x2006a6911a0, 0x0) runtime/syscall_windows.go:479 +0xf4 github.com/lxn/win.IsDialogMessage(0x2006a6911a0, 0x2006a6911a0) github.com/lxn/win@v0.0.0-20210218163916-a377121e959e/user32.go:2834 +0x65 github.com/lxn/walk.(FormBase).mainLoop(0xc00004f000) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/mainloop_default.go:35 +0xd7 github.com/lxn/walk.(FormBase).Run(0xc00004f000) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/form.go:375 +0x1af github.com/lxn/walk/declarative.MainWindow.Run({{{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/declarative/mainwindow.go:213 +0x94 main.Run() example.com/test/main.go:29 +0x225 main.main() example.com/test/main.go:9 +0x17

I understand it's not the right way, of course, but I don't know the right way.

lpintes commented 2 years ago

I would add

AssignTo: &w

to the MainWindow declaration.

Dňa 2. 12. 2021 o 12:04 xcd0 napísal(a):

I would like to know how to close the window normally when the button is clicked. My source code below ends with an exception.

package main

import ( ."github.com/lxn/walk/declarative" )

func main() { Run() // something Run() }

func Run() { var w MainWindow w = MainWindow{ Title:"test", Size:Size{400,200}, Layout:VBox{}, Children: []Widget{ PushButton{ Text:"Exit", OnClicked:func() { (**w.AssignTo).Close() }, }, }, } w.Run() }

panic: runtime error: invalid memory address or nil pointer
dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x64de7f]

goroutine 1 [running, locked to thread]:
main.Run.func1()
example.com/test/main.go:24 +0x1f
github.com/lxn/walk.(*EventPublisher).Publish(0xc00004fb30)
***@***.***/event.go:71
+0xf0
github.com/lxn/walk.(*Button).raiseClicked(...)
***@***.***/button.go:195
github.com/lxn/walk.(*Button).WndProc(0xc00004f800, 0xc00011a5d0,
0x111, 0x6, 0x5c10f4)
***@***.***/button.go:210
+0x7f
github.com/lxn/walk.(*PushButton).WndProc(0xc00004f800, 0x6,
0x111, 0x0, 0xc00004f000)
***@***.***/pushbutton.go:115
+0x16b
github.com/lxn/walk.(*ContainerBase).WndProc(0xc00006f180, 0x0,
0x111, 0x6, 0x5c10f4)
***@***.***/container.go:345
+0x46d
github.com/lxn/walk.defaultWndProc(0x0, 0x111, 0xc000114058,
0xc000113d58)
***@***.***/window.go:2159
+0xb9
syscall.Syscall6(0x7ffc7c0de3f0, 0x5, 0x7ffc4c4718f0, 0x5c10f4,
0x202, 0x0, 0x300db, 0x0)
runtime/syscall_windows.go:493 +0xfa
github.com/lxn/win.CallWindowProc(0xc00004f800, 0xc00004f000,
0x202, 0xc000000400, 0x0)
***@***.***/user32.go:2138
+0x86
github.com/lxn/walk.(*WindowBase).WndProc(0xc00004f800, 0x0,
0x202, 0x0, 0x300db)
***@***.***/window.go:2511
+0xc68
github.com/lxn/walk.(*Button).WndProc(0xc00004f800, 0xc0001145d0,
0x202, 0x0, 0x300db)
***@***.***/button.go:218
+0x117
github.com/lxn/walk.(*PushButton).WndProc(0xc00004f800,
0xc0001146b0, 0x202, 0xc000114938, 0xc000114638)
***@***.***/pushbutton.go:115
+0x16b
github.com/lxn/walk.defaultWndProc(0x0, 0x202, 0xc0001146b0, 0x0)
***@***.***/window.go:2159
+0xb9
syscall.Syscall(0x7ffc7c0dbd60, 0x2, 0x3212b4, 0x2006a6911a0, 0x0)
runtime/syscall_windows.go:479 +0xf4
github.com/lxn/win.IsDialogMessage(0x2006a6911a0, 0x2006a6911a0)
***@***.***/user32.go:2834
+0x65
github.com/lxn/walk.(*FormBase).mainLoop(0xc00004f000)
***@***.***/mainloop_default.go:35
+0xd7
github.com/lxn/walk.(*FormBase).Run(0xc00004f000)
***@***.***/form.go:375
+0x1af
github.com/lxn/walk/declarative.MainWindow.Run({{{0x0, 0x0}, {0x0,
0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, ...}, ...})
***@***.***/declarative/mainwindow.go:213
+0x94
main.Run()
example.com/test/main.go:29 +0x225
main.main()
example.com/test/main.go:9 +0x17

I understand it's not the right way, of course, but I don't know the right way.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lxn/walk/issues/795, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATNYX6Z3LENDNOFKV74PKTUO5HFLANCNFSM5JG2FZPA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

xcd0 commented 2 years ago

Thank you for your quick reply. When I added it according to the advice, the types did not match, so I wrote it as AssignTo: w.AssignTo, https://github.com/xcd0/walk-test/blob/main/main.go#L19 However, like the last time, the exception ends. Was my change wrong?

panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0x94de7f]

goroutine 1 [running, locked to thread]: main.Run.func1() example.com/test/main.go:24 +0x1f github.com/lxn/walk.(EventPublisher).Publish(0xc000049330) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/event.go:71 +0xf0 github.com/lxn/walk.(Button).raiseClicked(...) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/button.go:195 github.com/lxn/walk.(Button).WndProc(0xc000049000, 0xc0000da5a0, 0x111, 0x6, 0x1a0dea) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/button.go:210 +0x7f github.com/lxn/walk.(PushButton).WndProc(0xc000049000, 0x6, 0x111, 0x0, 0xc000080400) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/pushbutton.go:115 +0x16b github.com/lxn/walk.(ContainerBase).WndProc(0xc000064380, 0x0, 0x111, 0x6, 0x1a0dea) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/container.go:345 +0x46d github.com/lxn/walk.defaultWndProc(0x0, 0x111, 0xc0000ca058, 0xc0000c9d58) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:2159 +0xb9 syscall.Syscall6(0x7ffc1623e3f0, 0x5, 0x7ffc070318f0, 0x1a0dea, 0x202, 0x0, 0x11006d, 0x0) runtime/syscall_windows.go:493 +0xfa github.com/lxn/win.CallWindowProc(0xc000049000, 0xc000080400, 0x202, 0xc000000400, 0x0) github.com/lxn/win@v0.0.0-20210218163916-a377121e959e/user32.go:2138 +0x86 github.com/lxn/walk.(WindowBase).WndProc(0xc000049000, 0x0, 0x202, 0x0, 0x11006d) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:2511 +0xc68 github.com/lxn/walk.(Button).WndProc(0xc000049000, 0xc0000ca5d0, 0x202, 0x0, 0x11006d) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/button.go:218 +0x117 github.com/lxn/walk.(PushButton).WndProc(0xc000049000, 0xc0000ca6b0, 0x202, 0xc0000ca938, 0xc0000ca638) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/pushbutton.go:115 +0x16b github.com/lxn/walk.defaultWndProc(0x0, 0x202, 0xc0000ca6b0, 0x0) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/window.go:2159 +0xb9 syscall.Syscall(0x7ffc1623bd60, 0x2, 0x280db0, 0x29e7ee4cf60, 0x0) runtime/syscall_windows.go:479 +0xf4 github.com/lxn/win.IsDialogMessage(0x29e7ee4cf60, 0x29e7ee4cf60) github.com/lxn/win@v0.0.0-20210218163916-a377121e959e/user32.go:2834 +0x65 github.com/lxn/walk.(FormBase).mainLoop(0xc000080400) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/mainloop_default.go:35 +0xd7 github.com/lxn/walk.(FormBase).Run(0xc000080400) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/form.go:375 +0x1af github.com/lxn/walk/declarative.MainWindow.Run({{{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}) github.com/lxn/walk@v0.0.0-20210112085537-c389da54e794/declarative/mainwindow.go:213 +0x94 main.Run() example.com/test/main.go:29 +0x230 main.main() example.com/test/main.go:8 +0x17

lpintes commented 2 years ago

Oh yes.

w = &MainWindow{

AssignTo: &w

...

}

Could work.

Dňa 2. 12. 2021 o 15:16 xcd0 napísal(a):

Thank you for your quick reply. When I added it according to the advice, the types did not match, so I wrote it as |AssignTo: w.AssignTo,| https://github.com/xcd0/walk-test/blob/main/main.go#L19 However, like the last time, the exception ends. Was my change wrong?

panic: runtime error: invalid memory address or nil pointer
dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x94de7f]

goroutine 1 [running, locked to thread]:
main.Run.func1()
example.com/test/main.go:24 +0x1f
github.com/lxn/walk.(*EventPublisher).Publish(0xc000049330)
***@***.***/event.go:71
+0xf0
github.com/lxn/walk.(*Button).raiseClicked(...)
***@***.***/button.go:195
github.com/lxn/walk.(*Button).WndProc(0xc000049000, 0xc0000da5a0,
0x111, 0x6, 0x1a0dea)
***@***.***/button.go:210
+0x7f
github.com/lxn/walk.(*PushButton).WndProc(0xc000049000, 0x6,
0x111, 0x0, 0xc000080400)
***@***.***/pushbutton.go:115
+0x16b
github.com/lxn/walk.(*ContainerBase).WndProc(0xc000064380, 0x0,
0x111, 0x6, 0x1a0dea)
***@***.***/container.go:345
+0x46d
github.com/lxn/walk.defaultWndProc(0x0, 0x111, 0xc0000ca058,
0xc0000c9d58)
***@***.***/window.go:2159
+0xb9
syscall.Syscall6(0x7ffc1623e3f0, 0x5, 0x7ffc070318f0, 0x1a0dea,
0x202, 0x0, 0x11006d, 0x0)
runtime/syscall_windows.go:493 +0xfa
github.com/lxn/win.CallWindowProc(0xc000049000, 0xc000080400,
0x202, 0xc000000400, 0x0)
***@***.***/user32.go:2138
+0x86
github.com/lxn/walk.(*WindowBase).WndProc(0xc000049000, 0x0,
0x202, 0x0, 0x11006d)
***@***.***/window.go:2511
+0xc68
github.com/lxn/walk.(*Button).WndProc(0xc000049000, 0xc0000ca5d0,
0x202, 0x0, 0x11006d)
***@***.***/button.go:218
+0x117
github.com/lxn/walk.(*PushButton).WndProc(0xc000049000,
0xc0000ca6b0, 0x202, 0xc0000ca938, 0xc0000ca638)
***@***.***/pushbutton.go:115
+0x16b
github.com/lxn/walk.defaultWndProc(0x0, 0x202, 0xc0000ca6b0, 0x0)
***@***.***/window.go:2159
+0xb9
syscall.Syscall(0x7ffc1623bd60, 0x2, 0x280db0, 0x29e7ee4cf60, 0x0)
runtime/syscall_windows.go:479 +0xf4
github.com/lxn/win.IsDialogMessage(0x29e7ee4cf60, 0x29e7ee4cf60)
***@***.***/user32.go:2834
+0x65
github.com/lxn/walk.(*FormBase).mainLoop(0xc000080400)
***@***.***/mainloop_default.go:35
+0xd7
github.com/lxn/walk.(*FormBase).Run(0xc000080400)
***@***.***/form.go:375
+0x1af
github.com/lxn/walk/declarative.MainWindow.Run({{{0x0, 0x0}, {0x0,
0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, ...}, ...})
***@***.***/declarative/mainwindow.go:213
+0x94
main.Run()
example.com/test/main.go:29 +0x230
main.main()
example.com/test/main.go:8 +0x17

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/lxn/walk/issues/795#issuecomment-984666624, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATNYX4VGAHNWUQAAL6YAA3UO55SJANCNFSM5JG2FZPA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

xcd0 commented 2 years ago

Thank you for your quick reply. I don't know if I can change it like the advice, but if I rewrite it to AssignTo: & w like the commit below, I get a build error. https://github.com/xcd0/walk-test/commit/9b8d03d152cd19d2d4419c202d77b00dd63a2152

go build -o ./test.exe -trimpath # example.com/test .\main.go:19:3: cannot use &w (type declarative.MainWindow) as type walk.MainWindow in field value make: *** [makefile:5: all] Error 2

lpintes commented 2 years ago

Here is the fixed main.go:

packagemain import( "github.com/lxn/walk"     . "github.com/lxn/walk/declarative" ) funcmain() { // something Run() } funcRun() { varmwwalk.MainWindow w:= &MainWindow{         Title: "test",         Size:     Size{400, 200},         Layout:   VBox{},         AssignTo: &mw,         Children: []Widget{             PushButton{                 Text: "Exit",                 OnClicked: func() {                     mw.Close()                 },             },         },     }     (w).Run() } Dňa 2. 12. 2021 o 15:54 xcd0 napísal(a):

Thank you for your quick reply. I don't know if I can change it like the advice, but if I rewrite it to |AssignTo: & w| like the commit below, I get a build error. @.*** https://github.com/xcd0/walk-test/commit/9b8d03d152cd19d2d4419c202d77b00dd63a2152

go build -o ./test.exe -trimpath
# example.com/test
.\main.go:19:3: cannot use &w (type **declarative.MainWindow) as
type **walk.MainWindow in field value
make: *** [makefile:5: all] Error 2

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/lxn/walk/issues/795#issuecomment-984701352, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATNYX7ICYYYELDIKDEZDGTUO6CB5ANCNFSM5JG2FZPA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

xcd0 commented 2 years ago

Oh it worked thank you very much

https://github.com/xcd0/walk-test/blob/2c598860374ed1da0bae05b586953dfb8725fb6e/main.go