Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我能';在画布中的帧中滚动网格(tkinter)_Python_Tkinter_Canvas_Scroll_Grid - Fatal编程技术网

Python 我能';在画布中的帧中滚动网格(tkinter)

Python 我能';在画布中的帧中滚动网格(tkinter),python,tkinter,canvas,scroll,grid,Python,Tkinter,Canvas,Scroll,Grid,我正在使用Tkinter进行一个项目,我在Stackoveflow中发现了一个创建滚动条的代码,我对此很了解。我想滚动一些按钮,但现在当我尝试滚动一些网格(按钮和比例)时,它不起作用 我试图用答案和答案来管理我的代码,但我没有达到我的目标 如果你能帮助我,我将非常高兴,非常感谢 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # -------------------------------- Importation ----------------

我正在使用Tkinter进行一个项目,我在Stackoveflow中发现了一个创建滚动条的代码,我对此很了解。我想滚动一些按钮,但现在当我尝试滚动一些网格(按钮和比例)时,它不起作用

我试图用答案和答案来管理我的代码,但我没有达到我的目标

如果你能帮助我,我将非常高兴,非常感谢

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# -------------------------------- Importation ------------------------------- #

import tkinter as tk
from tkinter import messagebox

# ------------------------------ Initialisation ------------------------------ #

root = tk.Tk()

width_screen, height_screen = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (width_screen, height_screen))



# ----------------------- Creation of a list of sounds ----------------------- #

wav_files = ["a.wav","b.wav","c.wav","d.wav","e.wav","f.wav","g.wav","h.wav","i.wav","j.wav","k.wav","l.wav","m.wav","n.wav","o.wav","p.wav","q.wav","r.wav","s.wav","t.wav","u.wav","v.wav","w.wav","x.wav","y.wav","z.wav","aa.wav","bb.wav","cc.wav","dd.wav","ee.wav","ff.wav"]

# -------------------------- Vertical scrolled frame ------------------------- #

class VerticalScrolledFrame(tk.Frame):

    def __init__(self, parent, *args, **kw):
        tk.Frame.__init__(self, parent, *args, **kw)            

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
        vscrollbar.grid(row=0, column=1, sticky='ns')
        canvas = tk.Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set, width=root.winfo_screenwidth(), height=root.winfo_screenheight())
        canvas.grid(row=0, column=0, sticky="news")
        vscrollbar.config(command=canvas.yview)

        # reset the view
        #canvas.xview_moveto(0)
        #canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = tk.Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=tk.NW)

        # track changes to the canvas and frame width and sync them,
        # also updating the scrollbar
        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())

        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)


# ------------------------------- Sound buttons ------------------------------ #


class Make_sound:
    def __init__(self, name, parent):
        self.varbutton = tk.StringVar()
        self.varbutton.set("OFF")
        self.name = name
        self.parent = parent

        self.soundbuttoncreator()


    def launchsound(self):
        if self.varbutton.get() == "OFF":
            self.varbutton.set("ON")
        else:
            self.varbutton.set("OFF")

    def soundbuttoncreator(self):

        self.volumescale = tk.Scale(self.parent, orient='vertical', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)', )
        self.volumescale.grid(row=0,column=1, rowspan=2, sticky="nsew")
        self.faderscale = tk.Scale(self.parent, orient='horizontal', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)')
        self.faderscale.grid(row=1,column=0, sticky="nsew")
        self.button = tk.Checkbutton(self.parent,text=self.name, indicatoron=False, selectcolor="green", background="red", onvalue="ON", offvalue="OFF")
        self.button.grid(row=0, column=0, sticky="nsew")

def sounds_buttons(parent):
    for i in range(len(wav_files)):
        new_name = wav_files[i][:-4]
        globals()["wav_files"][i] = Make_sound(new_name,parent)

# ---------------------------------------------------------------------------- #
#                                   Creation                                   #
# ---------------------------------------------------------------------------- #

# ----------------------------- Buttons of sound ----------------------------- #

scframe = VerticalScrolledFrame(root)
scframe.pack(side=tk.LEFT)

sounds_buttons(scframe.interior)

root.mainloop()
#/usr/bin/env蟒蛇3
#-*-编码:utf-8-*-
#-------------------------------------进口------------------#
将tkinter作为tk导入
从tkinter导入消息框
#------------------------------------初始化------------------#
root=tk.tk()
宽度屏幕,高度屏幕=root.winfo屏幕宽度(),root.winfo屏幕高度()
根几何体(“%dx%d+0+0”%(宽度屏幕、高度屏幕))
#--------------------------创建声音列表-----------------#
wav_文件=[“a.wav”、“b.wav”、“c.wav”、“d.wav”、“e.wav”、“f.wav”、“g.wav”、“h.wav”、“i.wav”、“j.wav”、“k.wav”、“l.wav”、“m.wav”、“n.wav”、“o.wav”、“p.wav”、“q.wav”、“r.wav”、“s.wav”、“t.wav”、“u.wav”、“v”、“w.wav”、“w”、“x.wav”、“y.wav”、“z.wav”、“d”、“aa.wav”、“bb.wav”、“cc”、“wav”]
#------------------------------垂直滚动框-----------------#
类垂直滚动框架(tk.Frame):
定义初始值(自、父、*args、**kw):
tk.Frame.\uuuuuu init\uuuuuuuuuuuuuuuuuuuu(自、父、*args、**kw)
#创建画布对象和用于滚动的垂直滚动条
vscrollbar=tk.Scrollbar(self,orient=tk.VERTICAL)
vscrollbar.grid(行=0,列=1,sticky='ns')
canvas=tk.canvas(self,bd=0,highlightthickness=0,
yscrollcommand=vscrollbar.set,width=root.winfo_screenwidth(),height=root.winfo_screenheight())
canvas.grid(行=0,列=0,sticky=“news”)
vscrollbar.config(命令=canvas.yview)
#重置视图
#canvas.xview_moveto(0)
#canvas.yview_moveto(0)
#在画布内创建一个框架,该框架将与其一起滚动
self.interior=interior=tk.Frame(画布)
内部=画布。创建窗口(0,0,窗口=内部,
锚点=塔克西北)
#跟踪画布和帧宽度的更改并同步它们,
#同时更新滚动条
def_配置_内部(事件):
#更新滚动条以匹配内部框架的大小
大小=(interior.winfo_reqwidth(),interior.winfo_reqheight())
canvas.config(scrollregion=“0%s%s”%size)
if interior.winfo_reqwidth()!=canvas.winfo_width():
#更新画布的宽度以适合内部框架
canvas.config(width=interior.winfo_reqwidth())
内部绑定(“”,\u配置\u内部)
定义配置画布(事件):
if interior.winfo_reqwidth()!=canvas.winfo_width():
#更新内部框架的宽度以填充画布
canvas.itemconfigure(interior\u id,width=canvas.winfo\u width())
canvas.bind(“”,\u configure\u canvas)
#------------------------------------声音按钮------------------#
课堂录音:
定义初始化(自我、姓名、父项):
self.varbutton=tk.StringVar()
self.varbutton.set(“OFF”)
self.name=名称
self.parent=parent
self.soundbuttoncreator()
def启动声音(自):
如果self.varbutton.get()=“关闭”:
self.varbutton.set(“开”)
其他:
self.varbutton.set(“OFF”)
def soundbuttoncreator(自身):
self.volumescale=tk.Scale(self.parent,orient='vertical',从0到10,分辨率=0.1,滴答声间隔=2,标签='Volume(db)'))
self.volumescale.grid(行=0,列=1,行span=2,sticky=“nsew”)
self.faderscale=tk.Scale(self.parent,orient='horizontal',从0到10,分辨率=0.1,滴答声间隔=2,标签='Volume(db)'
self.faderscale.grid(行=1,列=0,sticky=“nsew”)
self.button=tk.Checkbutton(self.parent,text=self.name,indicatoron=False,选择color=“绿色”,background=“红色”,onvalue=“开”,offvalue=“关”)
self.button.grid(行=0,列=0,sticky=“nsew”)
def sounds_按钮(父级):
对于范围内的i(len(wav_文件)):
新名称=wav文件[i][:-4]
globals()[“wav_文件”][i]=发出声音(新名称,父项)
# ---------------------------------------------------------------------------- #
#创作#
# ---------------------------------------------------------------------------- #
#------------------------------------声音按钮------------------#
scframe=垂直滚动框架(根)
scframe.pack(侧面=左侧)
声音按钮(scframe.内饰)
root.mainloop()
编辑1: 似乎输出只是最后一个网格,因为显示的唯一按钮中的文本是ff,它对应于列表中的最后一个声音。 我没有这个问题,只是按钮(而不是网格):我可以看到每一个声音,我有滚动条活动

编辑2: 问题解决了,但现在我们遇到了一个新问题:向checkbutton发送命令是不可能的。你可以自己看到self.varbutton.set(“OFF”)不受尊重,按钮总是开着。。。 以下是有关命令问题的代码:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# -------------------------------- Importation ------------------------------- #

import tkinter as tk
from tkinter import messagebox

# ------------------------------ Initialisation ------------------------------ #

root = tk.Tk()

width_screen, height_screen = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (width_screen, height_screen))



# ----------------------- Creation of a list of sounds ----------------------- #

wav_files = ["a.wav","b.wav","c.wav","d.wav","e.wav","f.wav","g.wav","h.wav","i.wav","j.wav","k.wav","l.wav","m.wav","n.wav","o.wav","p.wav","q.wav","r.wav","s.wav","t.wav","u.wav","v.wav","w.wav","x.wav","y.wav","z.wav","aa.wav","bb.wav","cc.wav","dd.wav","ee.wav","ff.wav"]

# -------------------------- Vertical scrolled frame ------------------------- #

class VerticalScrolledFrame(tk.Frame):

    def __init__(self, parent, *args, **kw):
        tk.Frame.__init__(self, parent, *args, **kw)            

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
        vscrollbar.grid(row=0, column=1, sticky='ns')
        canvas = tk.Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set, width=root.winfo_screenwidth(), height=root.winfo_screenheight())
        canvas.grid(row=0, column=0, sticky="news")
        vscrollbar.config(command=canvas.yview)

        # reset the view
        #canvas.xview_moveto(0)
        #canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = tk.Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=tk.NW)

        # track changes to the canvas and frame width and sync them,
        # also updating the scrollbar
        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())

        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)


# ------------------------------- Sound buttons ------------------------------ #


class Make_sound:
    def __init__(self, name, parent):
        self.varbutton = tk.StringVar()
        self.name = name
        self.parent = parent

        self.soundbuttoncreator()


    def launchsound(self):
        print(self.varbutton.get())
        if self.varbutton.get() == 1:
            self.list=[]
        else:
            self.list.append("A")

    def soundbuttoncreator(self):
        self.frame = tk.Frame(self.parent) # create a frame to hold the widgets
        
        # use self.frame as parent instead of self.parent
        self.volumescale = tk.Scale(self.frame, orient='vertical', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)', )
        self.volumescale.grid(row=0,column=1, rowspan=2, sticky="nsew")
        self.faderscale = tk.Scale(self.frame, orient='horizontal', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)')
        self.faderscale.grid(row=1,column=0, sticky="nsew")
        self.button = tk.Checkbutton(self.frame, text=self.name, indicatoron=False, selectcolor="green", background="red", variable=self.varbutton, command=self.launchsound) 
        self.button.grid(row=0, column=0, sticky="nsew")

        self.frame.pack()  # use pack() on the frame so new instance of `Make_sound` will not overlap the old instances


def sounds_buttons(parent):
    for i in range(len(wav_files)):
        new_name = wav_files[i][:-4]
        globals()["wav_files"][i] = Make_sound(new_name,parent)

# ---------------------------------------------------------------------------- #
#                                   Creation                                   #
# ---------------------------------------------------------------------------- #

# ----------------------------- Buttons of sound ----------------------------- #

scframe = VerticalScrolledFrame(root)
scframe.pack(side=tk.LEFT)

sounds_buttons(scframe.interior)

root.mainloop()
!/usr/bin/env python3
#-*-编码:utf-8-*-
#-------------------------------------进口------------------#
将tkinter作为tk导入
从tkinter导入消息框
#------------------------------------初始化------------------#
root=tk.tk()
宽度屏幕,高度屏幕=root.winfo屏幕宽度(),root.winfo屏幕高度()
根几何体(“%dx%d+0+0”%(宽度屏幕、高度屏幕))
#--------------------------创建声音列表-----------------#
wav_文件=[“a.wav”,“b”
def soundbuttoncreator(self):

    self.volumescale = tk.Scale(self.parent, orient='vertical', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)', )
    self.volumescale.grid(row=0,column=1, rowspan=2, sticky="nsew") # same for all instances of Make_sound
    self.faderscale = tk.Scale(self.parent, orient='horizontal', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)')
    self.faderscale.grid(row=1,column=0, sticky="nsew") # same for all instances of Make_sound
    self.button = tk.Checkbutton(self.parent,text=self.name, indicatoron=False, selectcolor="green", background="red", onvalue="ON", offvalue="OFF")
    self.button.grid(row=0, column=0, sticky="nsew") # same for all instances of Make_sound
def soundbuttoncreator(self):
    self.frame = tk.Frame(self.parent) # create a frame to hold the widgets
    
    # use self.frame as parent instead of self.parent
    self.volumescale = tk.Scale(self.frame, orient='vertical', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)', )
    self.volumescale.grid(row=0,column=1, rowspan=2, sticky="nsew")
    self.faderscale = tk.Scale(self.frame, orient='horizontal', from_=0, to=10, resolution=0.1, tickinterval=2, label='Volume (db)')
    self.faderscale.grid(row=1,column=0, sticky="nsew")
    self.button = tk.Checkbutton(self.frame, text=self.name, indicatoron=False, selectcolor="green", background="red",
                                 onvalue="ON", offvalue="OFF", variable=self.varbutton) 
    self.button.grid(row=0, column=0, sticky="nsew")

    self.frame.pack()  # use pack() on the frame so new instance of `Make_sound` will not overlap the old instances