I want to be able to scroll in Tkinter

Asked 2 years ago, Updated 2 years ago, 100 views

Thank you for your continuous support.

Using Python 2.7's Tkinter, the GUI app scrolls
You can do it (scroll the whole thing like when you look at a browser)
I want to add a function.
I tried to refer to various sites, but I couldn't solve it.
I want to be able to scroll vertically first, but
What should I do?
I would appreciate it if you could explain without using the class.

Sorry for the inconvenience, but I appreciate your cooperation.

python python2 tkinter gui

2022-09-30 19:51

1 Answers

It doesn't say what kind of Widget you want to deploy, so it's assumed, but if you want CanvasWidget to have Scroll Bar, for example,

  • Place the Scrollbar Widget and Canvas Widget on the Root Window
  • Add action to notify Canvas when you run Scrollbar in Scrollbar.config (command=Canvas.yview)
  • Set Scroll Range in Canvas.config(scrollregion=())
  • Add Canvas.config (yscrollcommand=Scrollbar.set) to notify Scrollbar of Canvas' range of motion

will work on the .

#-*-coding:utf-8-*-
import Tkinter as tk

root=tk.Tk()
root.geometry ("400x200")

# Generate and deploy Canvas Widget
canvas=tk.Canvas(root)
canvas.pack (side=tk.LEFT, fill=tk.BOTH)

# Generate and deploy Scrollbar
bar=tk.Scrollbar(root, orient=tk.VERTICAL)
bar.pack (side=tk.RIGHT, fill=tk.Y)

# Added action to notify Canvas of Scrollbar control
bar.config(command=canvas.yview)

# Set the Scroll Range for Canvas
canvas.config(scrollregion=(0,0,400,400))

# Added action to notify Screoobar of Canvas range
canvas.config (yscrollcommand=bar.set)

# write a suitable figure on Canvas
id=canvas.create_oval(10,10,370,370)
canvas.itemconfigure(id,fill='red')

root.mainloop()

Unfortunately, FrameWidget does not support Scrollbar, but I think we can do so by placing FrameWidget on CanvasWidget with Scrollbar above.

#-*-coding:utf-8-*-
import Tkinter as tk

root=tk.Tk()
root.geometry ("400x200")

# Generate Canvas Widget
canvas=tk.Canvas(root)
canvas.pack (side=tk.LEFT, fill=tk.BOTH)

# Generate and deploy Scrollbar
bar=tk.Scrollbar(root, orient=tk.VERTICAL)
bar.pack (side=tk.RIGHT, fill=tk.Y)

# Added action to notify Canvas of Scrollbar control
bar.config(command=canvas.yview)

# Set the Scroll Range for Canvas
canvas.config(scrollregion=(0,0,400,400))

# Added action to notify Screoobar of Canvas range
canvas.config (yscrollcommand=bar.set)

# Generate Frame Widget
frame = tk.Frame(canvas)

# Place Frame Widget on Canvas Widget
canvas.create_window(0,0), window=frame, anchor=tk.NW, width=canvas.cget('width')))

# Place appropriate content on Frame
tk.Label(frame, text="Hello World!!", font=(",24)) .pack()

root.mainloop()


2022-09-30 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.