<tfoot id='6WW9a'></tfoot>

      1. <i id='6WW9a'><tr id='6WW9a'><dt id='6WW9a'><q id='6WW9a'><span id='6WW9a'><b id='6WW9a'><form id='6WW9a'><ins id='6WW9a'></ins><ul id='6WW9a'></ul><sub id='6WW9a'></sub></form><legend id='6WW9a'></legend><bdo id='6WW9a'><pre id='6WW9a'><center id='6WW9a'></center></pre></bdo></b><th id='6WW9a'></th></span></q></dt></tr></i><div id='6WW9a'><tfoot id='6WW9a'></tfoot><dl id='6WW9a'><fieldset id='6WW9a'></fieldset></dl></div>
        <legend id='6WW9a'><style id='6WW9a'><dir id='6WW9a'><q id='6WW9a'></q></dir></style></legend>
        • <bdo id='6WW9a'></bdo><ul id='6WW9a'></ul>

        <small id='6WW9a'></small><noframes id='6WW9a'>

        Tkinter 网格中的滚动条

        时间:2023-07-23

        <small id='1dSIf'></small><noframes id='1dSIf'>

        <tfoot id='1dSIf'></tfoot>

          <tbody id='1dSIf'></tbody>
          • <bdo id='1dSIf'></bdo><ul id='1dSIf'></ul>
          • <legend id='1dSIf'><style id='1dSIf'><dir id='1dSIf'><q id='1dSIf'></q></dir></style></legend>

              1. <i id='1dSIf'><tr id='1dSIf'><dt id='1dSIf'><q id='1dSIf'><span id='1dSIf'><b id='1dSIf'><form id='1dSIf'><ins id='1dSIf'></ins><ul id='1dSIf'></ul><sub id='1dSIf'></sub></form><legend id='1dSIf'></legend><bdo id='1dSIf'><pre id='1dSIf'><center id='1dSIf'></center></pre></bdo></b><th id='1dSIf'></th></span></q></dt></tr></i><div id='1dSIf'><tfoot id='1dSIf'></tfoot><dl id='1dSIf'><fieldset id='1dSIf'></fieldset></dl></div>

                  本文介绍了Tkinter 网格中的滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:
                  在 Tkinter 的小部件网格中添加滚动条

                  在我的项目中,我想在一个窗口中显示我的结果,使用 Tkinter 作为 GUI.我使用网格小部件将它们放在一种表格中,并且窗口被分成两个不同的部分(用于不同的结果).但经过较长时间的运行,显示的结果数量超过了我的屏幕高度,所以我想在我的程序中添加一个滚动条.我已经在 stackoverflow 上查看了几个问题,最接近的答案是:

                  On my project, i want do display my results in a window, using Tkinter as GUI. I place them in a kind of table, using the grid widget, and the window is separated in two different parts (for different results). But after longer runs, the number of results displayed exceed the height of my screen, so i want to add a scrollbar to my program. I already looked into several questions here on stackoverflow, and the answer that has come closest was this:

                  为一组小部件添加滚动条Tkinter(只是为了让你知道我在寻找什么!)

                  Adding a scrollbar to a group of widgets in Tkinter (just to let You know what I am axactly looking for!)

                  但我无法将它应用到我的程序中,可能是因为我对 Python 比较陌生,有时认为我是一个有教程示例的弗兰肯斯坦博士.

                  I am not able to apply that to my program though, maybe because I am rather new to Python and sometimes think i am a Dr. Frankenstein with tutorial-examples.

                  我现在尝试了很多,但我无法让表格显示在画布中,可能只是我缺少一些小东西.

                  I tried a lot now, but i cant get the tables to be displayed in the canvas, possibly just some little thing i am missing.

                  我为我的程序创建了一个抽象示例(没有滚动条),所以您知道我在使用什么,也许您中的某个人可以帮助我将滚动条放在它所属的位置!

                  I created an abstract example of my program (without scrollbar) so You know what I am working with, maybe someone of You can help my getting that scrollbar where it belongs!

                  非常感谢!

                  示例代码:(运行)

                  import Tkinter as tk
                  toprow=1
                  botrow=1
                  class ProgramWindow(tk.Frame): 
                  
                      def __init__(self,name): 
                          self.name = name
                          tk.Frame.__init__(self,root)
                          self.pack()
                  
                          if name=="BotWin":
                              tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
                              tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)
                  
                          elif name=="TopWin":
                              tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
                              tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)
                  
                      def addrowTop(self,stuff,otherstuff):
                          global toprow
                  
                          textfield = tk.Text(self,width=30,height=1)
                          textfield.grid(row=toprow,column=0)
                          textfield.insert('0.0',stuff)
                  
                          textfield = tk.Text(self,width=20,height=1)
                          textfield.grid(row=toprow,column=1)
                          textfield.insert('0.0',otherstuff)
                  
                          toprow+=1
                  
                      def addrowBot(self,stuff,otherstuff):
                          global botrow
                  
                          textfield = tk.Text(self,width=30,height=1)
                          textfield.grid(row=botrow,column=0)
                          textfield.insert('0.0',stuff)
                  
                          textfield = tk.Text(self,width=20,height=1)
                          textfield.grid(row=botrow,column=1)
                          textfield.insert('0.0',otherstuff)
                  
                          botrow+=1
                  
                  def SomeProg():
                      for i in range(20):
                          if i%2==0:
                              stuff = "Stuff is "+str(i)
                              otherstuff=i*3
                              Wins[0].addrowTop(stuff,otherstuff)
                          elif i%2==1:
                              stuff = "Stuff is "+str(i)
                              otherstuff=i*4
                              Wins[1].addrowBot(stuff,otherstuff)
                  
                  
                  root = tk.Tk()
                  root.title("Stuff")
                  
                  Wins = [ ProgramWindow("TopWin"),ProgramWindow("BotWin")]
                  SomeProg()
                  
                  root.mainloop()
                  

                  我尝试添加滚动条的附加代码(基于上面链接中显示的示例).如果滚动条只显示在下部,那也没关系,因为那是结果很多的部分.)

                  additional code with my tries to add the scrollbar (based on example shown in link above). if the scrollbar is only shown in the lower part, that would be okay since thats the part with the many results.)

                  import Tkinter as tk
                  toprow=1
                  botrow=1
                  class ProgramWindow(tk.Frame): 
                  
                      def __init__(self,name): 
                          self.name = name
                          self.frame=tk.Frame.__init__(self,root)
                  
                  
                          if name=="BotWin":
                              tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
                              tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)
                  
                              self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
                              self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
                              self.canvas.configure(yscrollcommand=self.vsb.set)
                  
                              self.vsb.pack(side="right", fill="y")
                              self.canvas.pack(side="left", fill="both", expand=True)
                              self.canvas.create_window((4,4), window=self.frame)
                  
                              self.bind("<Configure>", self.OnFrameConfigure)
                  
                          elif name=="TopWin":
                              self.pack()
                              tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
                              tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)
                  
                      def addrowTop(self,stuff,otherstuff):
                          global toprow
                  
                          textfield = tk.Text(self,width=30,height=1)
                          textfield.grid(row=toprow,column=0)
                          textfield.insert('0.0',stuff)
                  
                          textfield = tk.Text(self,width=20,height=1)
                          textfield.grid(row=toprow,column=1)
                          textfield.insert('0.0',otherstuff)
                  
                          toprow+=1
                  
                      def OnFrameConfigure(self, event):
                          self.canvas.configure(scrollregion=self.frame.bbox("all"))
                  
                      def addrowBot(self,stuff,otherstuff):
                          global botrow
                  
                          textfield = tk.Text(self,width=30,height=1)
                          textfield.grid(row=botrow,column=0)
                          textfield.insert('0.0',stuff)
                  
                          textfield = tk.Text(self,width=20,height=1)
                          textfield.grid(row=botrow,column=1)
                          textfield.insert('0.0',otherstuff)
                  
                          botrow+=1
                  
                  def SomeProg():
                      for i in range(20):
                          if i%2==0:
                              stuff = "Stuff is "+str(i)
                              otherstuff=i*3
                              Wins[0].addrowTop(stuff,otherstuff)
                          elif i%2==1:
                              stuff = "Stuff is "+str(i)
                              otherstuff=i*4
                              Wins[1].addrowBot(stuff,otherstuff)
                  
                  
                  root = tk.Tk()
                  root.title("Stuff")
                  
                  Wins = [ ProgramWindow("TopWin"),ProgramWindow("BotWin")]
                  SomeProg()
                  
                  root.mainloop()
                  

                  推荐答案

                  使 Bryan Oakley 的回答 适应您的具体问题:

                  To adapt Bryan Oakley's answer to your specific problem:

                  • 以画布为父级创建框架
                  • 使用画布作为scrollregion
                  • 的参数

                  注意:在python中进行子类化时,不需要存储父__init__的结果,因为它是对自身进行操作的.

                  Note: when subclassing in python, you do not need to store the result of parent __init__ since it operate on self.

                  这是补丁:

                       def __init__(self,name): 
                           self.name = name
                  -        self.frame=tk.Frame.__init__(self,root)
                  
                           if name=="BotWin":
                  +            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
                  +            tk.Frame.__init__(self,self.canvas)
                               tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
                               tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)
                  -            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
                               self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
                               self.canvas.configure(yscrollcommand=self.vsb.set)
                  
                               self.vsb.pack(side="right", fill="y")
                               self.canvas.pack(side="left", fill="both", expand=True)
                  -            self.canvas.create_window((4,4), window=self.frame)
                  +            self.canvas.create_window((4,4), window=self)
                               self.bind("<Configure>", self.OnFrameConfigure)
                  
                           elif name=="TopWin":
                  +            self.frame=tk.Frame.__init__(self,root)
                               self.pack()
                               tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
                               tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)
                  @@ -41,7 +40,7 @@
                           toprow+=1
                  
                       def OnFrameConfigure(self, event):
                  -        self.canvas.configure(scrollregion=self.frame.bbox("all"))
                  +        self.canvas.configure(scrollregion=self.canvas.bbox("all"))
                  
                       def addrowBot(self,stuff,otherstuff):
                           global botrow
                  

                  这篇关于Tkinter 网格中的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:根据“grid_location",按钮有自己的坐标系.方法? 下一篇:使用“matplotlib.mlab.griddata"时程序挂起

                  相关文章

                  <small id='m7YmZ'></small><noframes id='m7YmZ'>

                  <tfoot id='m7YmZ'></tfoot>
                    <bdo id='m7YmZ'></bdo><ul id='m7YmZ'></ul>

                    <i id='m7YmZ'><tr id='m7YmZ'><dt id='m7YmZ'><q id='m7YmZ'><span id='m7YmZ'><b id='m7YmZ'><form id='m7YmZ'><ins id='m7YmZ'></ins><ul id='m7YmZ'></ul><sub id='m7YmZ'></sub></form><legend id='m7YmZ'></legend><bdo id='m7YmZ'><pre id='m7YmZ'><center id='m7YmZ'></center></pre></bdo></b><th id='m7YmZ'></th></span></q></dt></tr></i><div id='m7YmZ'><tfoot id='m7YmZ'></tfoot><dl id='m7YmZ'><fieldset id='m7YmZ'></fieldset></dl></div>

                      <legend id='m7YmZ'><style id='m7YmZ'><dir id='m7YmZ'><q id='m7YmZ'></q></dir></style></legend>