如何使用窗口中心的鼠标滚轮在python中使用tkinter滚动GUI

问题描述 投票:0回答:1

这段代码没问题,但是当鼠标指针位于页面中间时,我想像使用Chrome一样使用鼠标滚轮滚动页面。 enter image description here

import tkinter as tk
from random import randint


class ScrolledFrame(tk.Frame):

    def __init__(self, parent, vertical=True, horizontal=False):
        super().__init__(parent)
        self._canvas = tk.Canvas(self)
        self._canvas.grid(row=0, column=0, sticky='news')  # changed
        self._vertical_bar = tk.Scrollbar(self, orient='vertical', command=self._canvas.yview)
        if vertical:
            self._vertical_bar.grid(row=0, column=1, sticky='ns')
        self._canvas.configure(yscrollcommand=self._vertical_bar.set)
        self._horizontal_bar = tk.Scrollbar(self, orient='horizontal', command=self._canvas.xview)
        if horizontal:
            self._horizontal_bar.grid(row=1, column=0, sticky='we')
        self._canvas.configure(xscrollcommand=self._horizontal_bar.set)
        self._vertical_bar.config(command=self._canvas.yview)
        self.inner = tk.Frame(self._canvas, bg='red')
        self._window = self._canvas.create_window((0, 0), window=self.inner, anchor='nw')
        self.columnconfigure(0, weight=1)  # changed
        self.rowconfigure(0, weight=1)  # changed
        self.inner.bind('<Configure>', self.resize)
        self._canvas.bind('<Configure>', self.frame_width)

    def frame_width(self, event):
        canvas_width = event.width
        self._canvas.itemconfig(self._window, width=canvas_width)

    def resize(self, event=None):
        self._canvas.configure(scrollregion=self._canvas.bbox('all'))


class Question:
    def __init__(self, parent, question, answer):
        self.parent = parent
        self.question = question
        self.answer = answer
        self.create_widgets()

    def get_input(self):
        value = self.entry.get()
        print('value:', value)
        if value == self.answer:
            print("Right it's " + self.answer)
            self.label['text'] = self.question + "Right it's " + self.answer
        else:
            self.label['text'] = "Sorry, it was " + self.answer

    def create_widgets(self):
        self.labelframe = tk.LabelFrame(self.parent, text="Domanda:")
        self.labelframe.pack(fill="both", expand=True)
        self.label = tk.Label(self.labelframe, text=self.question)
        self.label.pack(expand=True, fill='both')
        self.entry = tk.Entry(self.labelframe)
        self.entry.pack()
        self.entry.bind("<Return>", lambda x: self.get_input())


root = tk.Tk()
root.title("Quiz")
root.geometry("400x300")
window = ScrolledFrame(root)
window.pack(expand=True, fill='both')

for i in range(10):
    one = randint(1, 10)
    two = randint(1, 10)
    Question(window.inner, "How is the result of {} + {} ?".format(one, two), str(one + two))

root.mainloop()
python tkinter scrollbar mousewheel
1个回答
0
投票

I found how to make it

好吧,我有一些问题要求如何制作它,但多亏了qazxsw poi我成功地使鼠标滚轮按照我的意图工作。如果它可以提供帮助,那么就是这里的例子。

https://code.activestate.com/recipes/580640-scrolling-frame-with-mouse-wheel-support/
© www.soinside.com 2019 - 2024. All rights reserved.