无法在Tkinter的主窗口中添加滚动条

时间:2019-08-23 07:20:10

标签: python canvas tkinter label scrollbar

我正在使用库tkinter(Python 3)创建一个图形用户界面。

下面的代码是我到目前为止编写的代码,因此调用了GUI,并且还创建了一些按钮,但是除了“帮助”按钮(本文所针对的对象)之外,尚未实现附加到“事件”的功能。

import tkinter
from tkinter import *
import tkinter.filedialog
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
import PIL
from PIL import Image, ImageTk


class Window(Frame):


    def __init__(self, master = None):

        self.master = master 
        self.f18 = Frame(master, width=300, height =20)
        self.f17 = Frame(master, width=300, height =20)
        self.ftf18 = Frame(master, width=300, height =20)
        self.ftf17 = Frame(master, width = 300, height = 20)
        self.sf = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray1")
        self.sf_17 = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray1")
        self.tf_18 = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray2")
        self.tf_17 = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray2")
        self.output_folder = Frame(master, width = 300, height = 30, relief = GROOVE, borderwidth = 4)
        self.blank_frame_before_last = Frame(master, width = 300, height = 30)
        self.run_help_quit_frame = Frame(master, width = 300, height = 30, relief = GROOVE, borderwidth = 4)


        self.tcm_name_18 = ''
        self.tcm_name_17 = ''
        self.tf_name_18 = ''
        self.tf_name_17 = ''
        self.out_folder = ''

        self.var = IntVar()

        def browse_button_tcm18():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tcm_18.config(text = filename)
            print(self.file_name)

        def browse_button_tcm17():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tcm_17.config(text = filename)
            print(self.file_name)

        def browse_button_tf18():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tf_18.config(text = filename)
            print(self.file_name)

        def browse_button_tf17():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tf_17.config(text = filename)
            print(self.file_name)

        def browse_folder():
            folder_name = askdirectory()
            self.output_folder = folder_name
            self.display_output_folder.config(text = folder_name)
            print(self.folder_name)

        def run():
            pass

        def quit():
            pass 

        def help():
            help_window = Toplevel(master)
            help_window.title("Help")
            help_window.geometry("500x800")
            app_h = Help_Window(help_window)
            help_window.mainloop()

        self.blank_label_0 = Label(self.master, text = "").pack(side=TOP, padx=5)
        self.blank_label = Label(self.sf, text = "", bg="SlateGray1").pack(side=TOP, padx=5)
        self.open_tcm_button = Button(self.sf, text = "Open..", command = browse_button_tcm18).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tcm_18 = Label(self.sf, width = 80, bg = "white", textvariable = self.tcm_name_18, relief = SUNKEN, anchor = W)
        self.display_tcm_18.pack(side=LEFT)
        self.tcm18_label = Label(self.sf, text = "Input1", bg="SlateGray1", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)#.place(relx=0.75, rely=0.04,anchor=W)
        self.sf.pack(side=TOP)
        self.f18.pack(side=TOP)

        self.blank_label_2 = Label(self.sf_17, text = "",  bg="SlateGray1").pack(side=TOP, padx=5)
        self.open_tcm_17_button = Button(self.sf_17, text = "Open..", command = browse_button_tcm17).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tcm_17 = Label(self.sf_17, width = 80, bg = "white", textvariable = self.tcm_name_17, relief = SUNKEN, anchor = W)
        self.display_tcm_17.pack(side=LEFT)
        self.tcm_17_label = Label(self.sf_17, text = "Input2", bg="SlateGray1", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)
        self.sf_17.pack(side=TOP)
        self.f17.pack(side=TOP)

        self.blank_label_3 = Label(self.tf_18, text = "", bg = "SlateGray2").pack(side=TOP, padx=5)
        self.open_tf_18_button = Button(self.tf_18, text = "Open..", command = browse_button_tf18).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tf_18 = Label(self.tf_18, width = 80, bg = "white", textvariable = self.tf_name_18, relief = SUNKEN, anchor = W)
        self.display_tf_18.pack(side=LEFT)
        self.tf_18_label = Label(self.tf_18, text = "Input3", bg="SlateGray2", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)
        self.tf_18.pack(side=TOP)
        self.ftf18.pack(side=TOP)

        self.blank_label_4 = Label(self.tf_17, text = "", bg = "SlateGray2").pack(side=TOP, padx=5)
        self.open_tf_17_button = Button(self.tf_17, text = "Open..", command = browse_button_tf17).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tf_17 = Label(self.tf_17, width = 80, bg = "white", textvariable = self.tf_name_17, relief = SUNKEN, anchor = W)
        self.display_tf_17.pack(side=LEFT)
        self.tf_17_label = Label(self.tf_17, text = "Input4", bg="SlateGray2", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)
        self.tf_17.pack(side=TOP)
        self.ftf17.pack(side = TOP)

        self.blank_label_5 = Label(self.output_folder, text = "").pack(side=TOP, padx = 5)
        self.open_output_folder_button = Button(self.output_folder, text = "Open..", command = browse_folder).pack(side = LEFT, padx = 5, pady = 10)
        self.display_output_folder = Label(self.output_folder, width = 80, bg = "white", textvariable = self.out_folder, relief = SUNKEN, anchor = W) 
        self.display_output_folder.pack(side = LEFT) 
        self.output_folder_label = Label(self.output_folder, text = "Output Folder", relief = GROOVE).place(relx = 0.70, rely = 0.20, anchor = W)
        self.output_folder.pack(side = TOP)

        self.blank_frame_before_last.pack(side = TOP)
        self.run_help_quit_frame.pack(side = TOP)
        self.quit_button = Button(self.run_help_quit_frame, text = "Run!", command = run).pack(side=LEFT, padx = 5, pady = 10)
        self.blank_label_6 = Label(self.run_help_quit_frame, text = " "*15).pack(side=LEFT, padx=5)
        self.run_button = Button(self.run_help_quit_frame, text = "Quit!", command = quit).pack(side = LEFT, padx = 5, pady = 10)
        self.blank_label_7 = Label(self.run_help_quit_frame, text = " "*15).pack(side=LEFT, padx=5)
        self.help_button = Button(self.run_help_quit_frame, text = "Help?", command = help).pack(side = LEFT, padx = 5, pady = 10)


class Help_Window(Window):


    def __init__(self, master = None):
        self.master = master

        #Adding scrollbar
        self.scrollbar = Scrollbar(self.master, orient = 'vertical')
        self.scrollbar.pack(side = RIGHT, fill = 'y')

        # Creating Title
        self.description_label = Label(master, text = "\nHelp for Tool",
                                font = "Verdana 14 bold").pack(side = TOP)

        # Creating introduction
        introduction = """\n
        This tool provides a basic workflow.\n
        """
        self.introduction_text = Label(master, width= 70, height = 5, justify = CENTER, padx = 5, 
                                        text = introduction, font = "Verdana 10").pack(side=TOP)

        # Adding TCM 2018
        self.tcm_2018_label = Label(master, text = "\nInput1\n", font = "Verdana 12 bold").pack(
                                    side = TOP)
        self.tcm_2018_img_canvas = Canvas(master, bg = "black", height = 300, width = 300)
        self.tcm_2018_img_canvas.pack(side = TOP)

        # Adding TCM 2017
        self.tcm_2017_label = Label(master, text = "\nInput 2\n", font = "Verdana 12 bold").pack(
                                    side = TOP)
        self.tcm_2017_img_canvas = Canvas(master, bg = "black", height = 300, width = 300)
        self.tcm_2017_img_canvas.pack(side = TOP)
        self.scrollbar.config(command = self.tcm_2017_img_canvas.yview)
        self.tcm_2017_img_canvas.config(yscrollcommand = self.scrollbar.set)
        #self.tcm_2017_img_canvas.pack()



root = Tk() 
root.title("Tool Title")
root.geometry("900x800")
app = Window(root) 
root.mainloop()

按“帮助”按钮时,您会看到一个“帮助类型”窗口。我尝试创建一个附加到此新窗口的滚动条。滚动条已显示,但不起作用。 我一直在搜索一些文档以及一些堆栈溢出帖子,并且发现了以下需要与您确认的信息。

Adding a scrollbar to a group of widgets in Tkinter

http://effbot.org/zone/tkinter-scrollbar-patterns.htm

是因为我正在创建Help_Window类并将滚动条添加到主窗口中,而不是做错了事情,否则首先创建画布,然后添加滚动条,其他将在其中写入内容的框架? 在进行修改Help_Window类的任务之前,我需要澄清一下。

1 个答案:

答案 0 :(得分:3)

您已经解决了问题。窗口对象不能成为滚动条的主对象。在某些情况下,滚动条的可能的母版可以是画布,文本,列表框甚至条目。我想所有人都有。 因此,如果要在几乎整个窗口上使用滚动条,只需制作一个“窗口”画布,其中将包含滚动条和该窗口的所有其他元素