2022年1月8日 星期六

Python-Tkinter Scrollbar

 Python-Tkinter Scrollbar

w = Scrollbar(master, options) 

Parameters:

  • master: This parameter is used to represents the parent window.
  • options: There are many options which are available and they can be used as key-value pairs separated by commas.

Options:
Following are commonly used Option can be used with this widget :-

  • activebackground: This option is used to represent the background color of the widget when it has the focus.
  • bg: This option is used to represent the background color of the widget.
  • bd: This option is used to represent the border width of the widget.
  • command: This option can be set to the procedure associated with the list which can be called each time when the scrollbar is moved.
  • cursor: In this option, the mouse pointer is changed to the cursor type set to this option which can be an arrow, dot, etc.
  • elementborderwidth: This option is used to represent the border width around the arrow heads and slider. The default value is -1.
  • Highlightbackground: This option is used to focus highlighcolor when the widget doesn’t have the focus.
  • highlighcolor: This option is used to focus highlighcolor when the widget has the focus.
  • highlightthickness: This option is used to represent the thickness of the focus highlight.
  • jump: This option is used to control the behavior of the scroll jump. If it set to 1, then the callback is called when the user releases the mouse button.
  • orient: This option can be set to HORIZONTAL or VERTICAL depending upon the orientation of the scrollbar.
  • repeatdelay: This option tells the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. The default is 300 ms.
  • repeatinterval: The default value of the repeat interval is 100.
  • takefocus: You can tab the focus through a scrollbar widget
  • troughcolor: This option is used to represent the color of the trough.
  • width: This option is used to represent the width of the scrollbar.

Methods:
Methods used in this widgets are as follows:

  • get(): This method is used to returns the two numbers a and b which represents the current position of the scrollbar.
  • set(first, last): This method is used to connect the scrollbar to the other widget w. The yscrollcommand or xscrollcommand of the other widget to this method.

from tkinter import *

root = Tk()

root.geometry("150x200")

w = Label(root, text ='GeeksForGeeks',

font = "50")

w.pack()

scroll_bar = Scrollbar(root)

scroll_bar.pack( side = RIGHT,

fill = Y )

mylist = Listbox(root,

yscrollcommand = scroll_bar.set )

for line in range(1, 26):

mylist.insert(END, "Geeks " + str(line))

mylist.pack( side = LEFT, fill = BOTH )

scroll_bar.config( command = mylist.yview )

root.mainloop()



from tkinter import *
root = Tk()
root.title('PythonGuides')
root.geometry('400x300')
root.config(bg='#4A7A8C')
frame = Frame(root)
frame.pack(expand=True, fill=BOTH, padx=20, pady=20)
lb = Listbox(
    frame,
    font= (12)
    )
lb.pack(expand=True, fill=BOTH, side=LEFT)
sb = Scrollbar(
    frame, 
    orient=VERTICAL
    )
sb.pack(fill=Y, side=RIGHT)
lb.configure(yscrollcommand=sb.set)
sb.config(command=lb.yview)
lb.insert(0, 'Fruits')
lb.insert(1,'Citrus – oranges, limes, grapefruits and mandarins')
lb.insert(2,'Stone fruit – apricots, plums, peaches and nectarines')
lb.insert(3,'Tropical and exotic – mangoes a bananasnd ')
lb.insert(4,'Berries – kiwifruit, raspberries, strawberries, blueberries,  and passionfruit')
lb.insert(5,'Melons – honeydew melons, watermelons and rockmelons')
lb.insert(6, 'Vegetables')
lb.insert(7, 'Leafy green – spinach and silverbeet, lettuce')
lb.insert(8, 'Cruciferous – cabbage, broccoli, Brussels sprouts and cauliflower')
lb.insert(9, 'Marrow – zucchini, cucumber and pumpkin')
lb.insert(10, 'Root – yam, sweet potato and potato')
lb.insert(11, 'Edible plant stem – asparagus and celery')
lb.insert(12, 'Allium – garlic, onion and shallot')
root.mainloop()

from tkinter import *
root = Tk()
root.title('PythonGuides')
root.geometry('400x300')
root.config(bg='#4A7A8C')
frame = Frame(root)
frame.pack(expand=True, fill=BOTH, padx=20, pady=20)
lb = Listbox(
    frame,
    font= (12)
    )
lb.pack(expand=True, fill=BOTH)
sb = Scrollbar(
    frame, 
    orient=HORIZONTAL
    )
sb.pack(fill=X)
lb.configure(xscrollcommand=sb.set)
sb.config(command=lb.xview)
lb.insert(0, 'Not all heros wear capes.')
lb.insert(1, 'Game changers are in blue')
lb.insert(2, 'With great power comes great responsibility')
lb.insert(3, 'A noun phrase has a noun or pronoun as the main word')
lb.insert(4, 'With great power comes great responsibility')
lb.insert(5, 'contribute to open source, as this will help & motivate you.')
root.mainloop()


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...