Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are scrollbars on frames doable? #27

Open
john157157 opened this issue Feb 12, 2022 · 1 comment
Open

Are scrollbars on frames doable? #27

john157157 opened this issue Feb 12, 2022 · 1 comment

Comments

@john157157
Copy link

john157157 commented Feb 12, 2022

Working off of Bryan Oakley's TKinter example here: https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter/3092341#3092341 I only got this far:

`
func NewWindow() *Window {
mw := &Window{tk.RootWindow()}
mw.ResizeN(800, 600)
canvas := tk.NewCanvas(mw, tk.WidgetAttrWidth(800), tk.WidgetAttrHeight(600), tk.CanvasAttrBorderWidth(0), tk.CanvasAttrBackground("white"))
frame := tk.NewFrame(canvas, tk.WidgetAttrWidth(750), tk.WidgetAttrHeight(270), tk.WidgetAttrInitUseTheme(false))
frame.SetNativeAttribute("background", "black")
// populate with some labels
for i := 0; i < 40; i++ {
lbl := tk.NewLabel(frame, "label "+strconv.Itoa(i))
lbl.SetBackground("#ccc")
tk.Grid(lbl, tk.GridAttrRow(i), tk.GridAttrColumn(0), tk.GridAttrPadx(5), tk.GridAttrPady(5), tk.GridAttrSticky(tk.StickyAll))
}
vertScrollbar := tk.NewScrollBar(mw, tk.Vertical, tk.WidgetAttrInitUseTheme(false))
vertScrollbar.SetNativeAttribute("command", "canvas yview") // bad window pathname error
tk.Grid(canvas, tk.GridAttrRow(0), tk.GridAttrColumn(0), tk.GridAttrSticky(tk.StickyAll))
tk.Grid(frame, tk.GridAttrRow(0), tk.GridAttrColumn(0), tk.GridAttrPadx(15), tk.GridAttrPady(15))
tk.Grid(vertScrollbar, tk.GridAttrRow(0), tk.GridAttrColumn(1), tk.GridAttrSticky(tk.StickyNS))
return mw
}

func main() {
tk.MainLoop(func() {
mw := NewWindow()
mw.SetTitle("ATK Sample")
mw.Center(nil)
mw.ShowNormal()
mw.BindKeyEvent(func(e *tk.KeyEvent) {
if e.Event.KeyCode == 9 { // esc key
tk.Quit()
}
})
})
}

The problem is these Python/TKinter lines:
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
frame.bind("", lambda event, canvas=canvas: onFrameConfigure(canvas))
`

Any help appreciated.

If the focus of the question feels too narrow then how about example code showing attaching a scrollbar to anything?

I'm sorry that my code isn't appropriately indented. I clicked the "<>" icon, copy/pasted and lost the indents.

@john157157
Copy link
Author

In tcl/tk it's possible. https://stackoverflow.com/questions/39956549/how-to-add-a-scrollbar-to-tcl-frame

This tcl/tk code is from Oliver Scholl's post. It produces a nice table on a frame with both scrollbars. The only changes I made were renaming the frame and canvas to improve readability.

So... Does anyone know how to get access to the canvas.yview and canvas.xview properties?

`
ttk::frame .frame

# create canvas with scrollbars
canvas .frame.canvas -width 400 -height 200 -xscrollcommand ".frame.xscroll set" -yscrollcommand ".frame.yscroll set"
ttk::scrollbar .frame.xscroll -orient horizontal -command ".frame.canvas xview"
ttk::scrollbar .frame.yscroll -command ".frame.canvas yview"
pack .frame.xscroll -side bottom -fill x
pack .frame.yscroll -side right -fill y
pack .frame.canvas -expand yes -fill both -side top

# create frame with widgets
ttk::frame .frame.canvas.frWidgets -borderwidth 1 -relief solid -width 340 -height 700

for {set i 0} {$i <=20} {incr i} {
ttk::label .frame.canvas.frWidgets.lb$i -text "Label $i:"
ttk::entry .frame.canvas.frWidgets.en$i
ttk::button .frame.canvas.frWidgets.bt$i -text "Button $i" -command exit
grid .frame.canvas.frWidgets.lb$i -padx 2 -pady 2 -row $i -column 0
grid .frame.canvas.frWidgets.en$i -padx 2 -pady 2 -row $i -column 1
grid .frame.canvas.frWidgets.bt$i -padx 2 -pady 2 -row $i -column 2
}

# create frame with buttons
ttk::frame .frame.canvas.frButtons -borderwidth 1 -relief solid -width 340 -height 40
ttk::button .frame.canvas.frButtons.btOK -text "OK" -command exit
ttk::button .frame.canvas.frButtons.btAbbruch -text "Abbruch" -command exit
pack .frame.canvas.frButtons.btOK -padx 2 -pady 2 -side left
pack .frame.canvas.frButtons.btAbbruch -padx 2 -pady 2 -side left

# place widgets and buttons
.frame.canvas create window 0 0 -anchor nw -window .frame.canvas.frWidgets
.frame.canvas create window 200 650 -anchor c -window .frame.canvas.frButtons

# determine the scrollregion
.frame.canvas configure -scrollregion [.frame.canvas bbox all]

# show the canvas
pack .frame -expand yes -fill both -side top
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant