当前位置: 代码迷 >> python >> Tkinter 滚动条——Python 3
  详细解决方案

Tkinter 滚动条——Python 3

热度:29   发布时间:2023-07-16 11:09:32.0

我最近一直在研究一个测验应用程序,现在我开始制作 GUI。 我想这样做,以便我可以有一个用于水平滚动的滚动条,而如果 GUI 长度大于某个长度,则会显示垂直滚动条。 唉,我只是一个 Tkinter 初学者,无法弄清楚滚动条。 请帮帮我 这是 GUI 的代码:

    def run(self):
        self.root1 = tk.Tk()
        self.root1.protocol("WM_DELETE_WINDOW", self.cont)
        self.root1.resizable(False, False)
        self.root1.title(TITLE)
        QAS_SEQUENCED = qasSequenced
        ANSWERS = list(QAS_SEQUENCED.values())
        QUESTIONS = list(QAS_SEQUENCED.keys())
        print(QUESTIONS)
        print(ANSWERS)
        global ANSWER_FIELDS
        ANSWER_FIELDS = []
        ANSWER_FIELDS.clear()
        self.REPLIES_AA = []
        qColumnIndex = 1 #Current Column (Qs relative)
        qColumnIndex_ALIGN = 1
        numColumns = int(len(QUESTIONS)/10) #Num columns (Qs total)
        ROW = 0
        QUESTION = ""
        for i in range(0, len(QUESTIONS)):
            QUESTION_TMP = QUESTIONS[i]
            QUESTION = ""
            QUESTION_NEWLINE_INDEX = int(len(QUESTION_TMP)/40)
            QUESTION_NEWLINE_INDEX_USE = 1
            for ii in range(0, len(QUESTION_TMP)):
                QUESTION_TMP = str(QUESTION_TMP)
                if (ii+1) >= 40*QUESTION_NEWLINE_INDEX_USE or QUESTION_TMP[ii] == "`":
                    QUESTION += ("\n")
                    if not QUESTION_TMP[ii] == "`":
                        QUESTION += str(QUESTION_TMP[ii])
                    QUESTION_NEWLINE_INDEX_USE += 1
                else:
                    QUESTION += str(QUESTION_TMP[ii])
            QUESTION = str(QUESTION)
            ANSWERS[i] = str(ANSWERS[i])
            print('Setting up question ' + str(i+1))
            print('Question: ' + QUESTION + ' Answer: ' + ANSWERS[i] + "\n")
            if ROW > 10:
                qColumnIndex += 1
                qColumnIndex_ALIGN += 2
                ROW = 0
            QUESTION_TMP = tk.Label(self.root1)
            QUESTION_TMP.config(text=QUESTION)
            QUESTION_TMP.grid(row=ROW,column=(qColumnIndex_ALIGN-1))
            ANSWER_TEMP_ASF = tk.Entry(self.root1)
            ANSWER_TEMP_ASF.grid(row=ROW,column=(qColumnIndex_ALIGN))
            ANSWER_FIELDS.append(ANSWER_TEMP_ASF)
            ROW += 1
        if len(QUESTIONS) <= 10:
            self.submitButton = tk.Button(self.root1,text='SUBMIT ANSWERS',command=self.submitClick)
            self.submitButton.grid(row=(len(QUESTIONS)+1),column=1)
        else:
            self.submitButton = tk.Button(self.root1, text='SUBMIT ANSWERS', command=self.submitClick)
            self.submitButton.grid(row=(len(QUESTIONS)+1),column=1)
        self.root1.mainloop()

阅读 -@stovfl 中

我阅读了上面提到的文章并对我的代码进行了一些简单的更改。 它们如下:

def onFrameConfigure(self, canvas):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def run(self):
        self.root1 = tk.Tk()
        self.root1.protocol("WM_DELETE_WINDOW", self.cont)
        self.root1.resizable(True, True)
        self.root1.title(TITLE)
        self.canvas = tk.Canvas(self.root1, borderwidth=0)
        self.frame = tk.Frame(self.canvas)
        self.vsb = tk.Scrollbar(self.root1, orient="vertical", command=self.canvas.yview)
        self.xsb = tk.Scrollbar(self.root1, orient="horizontal",command=self.canvas.xview)
        self.canvas.configure(yscrollcommand=self.vsb.set,xscrollcommand=self.xsb.set)
        self.vsb.pack(side="right", fill="y")
        self.xsb.pack(side="bottom",fill="x")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas.create_window((6,6),window=self.frame, anchor="nw")
        self.frame.bind("<Configure>", lambda event, canvas=self.canvas: self.onFrameConfigure(self.canvas))
        QAS_SEQUENCED = qasSequenced
        ANSWERS = list(QAS_SEQUENCED.values())
        QUESTIONS = list(QAS_SEQUENCED.keys())

        print('SETUP Frame')

        print(QUESTIONS)
        print(ANSWERS)

        global ANSWER_FIELDS
        ANSWER_FIELDS = []
        ANSWER_FIELDS.clear()

        self.REPLIES_AA = []
        print('Set self.REPLIES_AA')

        qColumnIndex = 1 #Current Column (Qs relative)
        qColumnIndex_ALIGN = 1
        numColumns = int(len(QUESTIONS)/10) #Num columns (Qs total)
        ROW = 0

        QUESTION = ""

        for i in range(0, len(QUESTIONS)):
            QUESTION_TMP = QUESTIONS[i]
            QUESTION = ""
            QUESTION_NEWLINE_INDEX = int(len(QUESTION_TMP)/40)
            QUESTION_NEWLINE_INDEX_USE = 1

            for ii in range(0, len(QUESTION_TMP)):
                QUESTION_TMP = str(QUESTION_TMP)
                if (ii+1) >= 40*QUESTION_NEWLINE_INDEX_USE or QUESTION_TMP[ii] == "`":
                    QUESTION += ("\n")
                    if not QUESTION_TMP[ii] == "`":
                        QUESTION += str(QUESTION_TMP[ii])
                    QUESTION_NEWLINE_INDEX_USE += 1
                else:
                    QUESTION += str(QUESTION_TMP[ii])

            QUESTION = str(QUESTION)
            ANSWERS[i] = str(ANSWERS[i])

            print('Setting up question ' + str(i+1))
            print('Question: ' + QUESTION + ' Answer: ' + ANSWERS[i] + "\n")

            if ROW > 10:
                qColumnIndex += 1
                qColumnIndex_ALIGN += 2
                ROW = 0

            QUESTION_TMP = tk.Label(self.frame)
            QUESTION_TMP.config(text=QUESTION)
            QUESTION_TMP.grid(row=ROW,column=(qColumnIndex_ALIGN-1))

            ANSWER_TEMP_ASF = tk.Entry(self.frame)
            ANSWER_TEMP_ASF.grid(row=ROW,column=(qColumnIndex_ALIGN))
            ANSWER_FIELDS.append(ANSWER_TEMP_ASF)
            ROW += 1
        if len(QUESTIONS) <= 10:
            self.submitButton = tk.Button(self.frame,text='SUBMIT ANSWERS',command=self.submitClick)
            self.submitButton.grid(row=(len(QUESTIONS)+1),column=1)
        else:
            self.submitButton = tk.Button(self.frame, text='SUBMIT ANSWERS', command=self.submitClick)
            self.submitButton.grid(row=(len(QUESTIONS)+1),column=1)
        self.root1.mainloop()

这些修改完全符合我的要求,现在我对这个 GUI 的结果感到非常高兴!

感谢所有回复和帮助的人。