带有Canvas的Python Tkinter滚动条不起作用

时间:2016-12-18 00:21:11

标签: python canvas tkinter scrollbar

我的智慧很低,也许我已经把头撞到墙上太多天了。

我想要一个带有可滚动框架(或画布)的顶级窗口,其中包含一组按钮,每个按钮都可以选择一种颜色。

除了我似乎无法让滚动条处理以下代码之外,其他所有工作都有效:

#!/usr/bin/env python

import Tkinter as tk

def AskForAColor(top_level):
    global picked_name
    picked_name = ''

    def OKGo(event = None):
        # Handle OK button

    def StopThis(event = None):
        # Handle CANCEL button

    def PickIt(c_namme):
        global picked_name
        picked_name = c_namme
        print 'picked color is ' + picked_name

    def UnNatural(colr):
        if colr < 128: return 255
        else: return 0

#================================================================
    f_bot = tk.Frame(top_level, background='yellow', bd = 10)
    f_bot.pack(side = 'bottom', expand=0, fill=tk.X)
    f_bot.pack_propagate(1)

# insert into the bottom, the buttons.
    tk.Button(f_bot, text='Cancel', command=StopThis).pack(side='right')
    tk.Button(f_bot, text='OK', command=OKGo).pack(side='right')

#================================================================
    f_mid = tk.Frame(top_level, background='blue', bd=8)
    f_mid.pack(side = 'top', expand=1, fill=tk.BOTH)
    f_mid.rowconfigure(0, weight=1)
    f_mid.columnconfigure(0, weight=1)

#setup v scrollbar
    y_scroll_bar = tk.Scrollbar(f_mid, orient=tk.VERTICAL)
    y_scroll_bar.grid(row=0, column=1, sticky='nsw')

#setup h scrollbar
    x_scroll_bar = tk.Scrollbar(f_mid, orient=tk.HORIZONTAL)
    x_scroll_bar.grid(row=1, column=0, sticky='ews')

# make a canvas
    canvas_of_colors = tk.Canvas(f_mid, background = 'green')
    canvas_of_colors.grid(row=0, column=0, sticky='nesw')
    canvas_of_colors.columnconfigure(0, weight=1)
    canvas_of_colors.rowconfigure(0, weight=1)
    canvas_of_colors['yscrollcommand'] = y_scroll_bar.set
    canvas_of_colors['xscrollcommand'] = x_scroll_bar.set
    canvas_of_colors.grid_propagate(1)

#continue the scrollbars
    y_scroll_bar.command=canvas_of_colors.yview
    x_scroll_bar.command=canvas_of_colors.xview

#make a frame for the canvas
    frame_of_buttons = tk.Frame(canvas_of_colors, background = 'orange')
    frame_of_buttons.pack()

    canvas_of_colors.create_window(0, 0, window=frame_of_buttons, anchor=tk.NW)

# now, insert a whole bunch of buttons
    with open('C:/Python27/Tools/pynche/X/rgb.txt') as f:
        content = f.readlines()
        roww = coll = 0
        for k in range(1, 64):
            things = content[k].split()
            color_name = things[3]
            butt = tk.Button(frame_of_buttons,
                text=color_name, 
                background = '#%02x%02x%02x' % (int(things[0]), int(things[1]), int(things[2])),
                foreground = '#%02x%02x%02x' % (UnNatural(int(things[0])), UnNatural(int(things[1])), UnNatural(int(things[2]))),
                command = lambda name=color_name: PickIt(name)
            )
            butt.grid(row=roww, column=coll, sticky = 'ew')
            butt.columnconfigure(coll, weight=1)
            butt.rowconfigure(roww, weight=1)

            coll += 1
            if coll > 7:
                coll = 0
                roww += 1

print "Starting"
root = tk.Tk()
root['background'] = 'black'

AskForAColor(root)

root.mainloop()

谢谢,Mark。

PS。使用包管理器时,rowconfigure / columnconfigure是否可以执行任何操作? 米。

0 个答案:

没有答案