whats the problem with my scrollbar ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moroccanplaya
    New Member
    • Jan 2011
    • 80

    whats the problem with my scrollbar ?

    my scroll bar works but the size is to small and how would i go by adding a horizontal scroll bar ? help would be appreciated thanks.

    Code:
    app = Tk()
    app.title(" text editor")
    
    content = ttk.Frame(app, padding=(3,3,12,12))
    content.grid(column=0, row=0,sticky=(N, S, E, W))
    
    #creat label
    labeltext = StringVar()
    labeltext.set("enter url:")
    label1 = ttk.Label(content, textvariable=labeltext).grid(column=0, row=1, columnspan=1, rowspan=1, sticky=(N, W), padx=5)
    
    #create text box
    urlname = StringVar()# text being enterd in tht text box is stored in urlname
    
    urlname_entry = ttk.Entry(content, textvariable=urlname)
    urlname_entry.grid(column=1, row=1, columnspan=3,rowspan=5, sticky=(N) )
    #focus in the text box so user dont have to click on
    urlname_entry.focus()
    #create button
    
    button1 = ttk.Button(content,text="get source", command=geturl)
    button2 = ttk.Button(content,text="count white spaces", command=count_white_space)
    button1.grid(column=3,row=0, columnspan=1, rowspan=2, sticky=(N,W))
    button2.grid(column=4,row=0,columnspan=2, rowspan=2, sticky=(N,W))
    
    
    scroll = tkinter.Scrollbar(content,borderwidth=2)
    Text = tkinter.Text(content,wrap=CHAR, width=50, height=20)
    
    scroll.config(command=Text.yview)
    Text.config(yscrollcommand=scroll.set)
    
    Text.grid(row=2, column=1,columnspan=1, rowspan=3, sticky=(N))
    scroll.grid(row=2,column=3)
    
    app.columnconfigure(0, weight=1)
    app.rowconfigure(0, weight=1)
    content.columnconfigure(0, weight=3)
    content.columnconfigure(1, weight=3)
    content.columnconfigure(2, weight=3)
    content.columnconfigure(3, weight=1)
    content.columnconfigure(4, weight=1)
    content.rowconfigure(1, weight=1)
    
    
    
    #adds spacing between widgets
    for child in app.winfo_children(): child.grid_configure(padx=5, pady=5)
    for child in content.winfo_children(): child.grid_configure(padx=5, pady=5)
    
    app.bind('<Return>',geturl) #enter can also be hit
    
    
    
    app.mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Scrollbar.grid needs to be configured sticky='ns' and rowspan=3. A horizontal scrollbar will be added the same way but configured orient=Tkinter. HORIZONTAL and w.grid sticky="ew". Configure the Text widget xscrollcommand= hscrollbar.set and hscrollbar["command"] = self.textWidget .xview.

    I posted an example Text widget with horizontal and vertical scrollbars in this thread.

    Comment

    • moroccanplaya
      New Member
      • Jan 2011
      • 80

      #3
      so do i need to re write my whole code so it can include classes and self, i haven't used classes before in python

      Comment

      • moroccanplaya
        New Member
        • Jan 2011
        • 80

        #4
        and im using python 3, do you want to see my full sourcecode

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          You don't have to use classes. Have you tried some of my suggestions?

          Comment

          • moroccanplaya
            New Member
            • Jan 2011
            • 80

            #6
            do you think its best to switch to a previous version of python as non of the examples in http://infohost.nmt.edu/tcc/help/pubs/tkinter.pdf work in python 3

            Comment

            • moroccanplaya
              New Member
              • Jan 2011
              • 80

              #7
              yes i have i have added a horizontal scroll bar but its not working with the text widget only the vertical scroll bar is working

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Post your code and I will take a look.

                Comment

                • moroccanplaya
                  New Member
                  • Jan 2011
                  • 80

                  #9
                  Code:
                  #!/usr/bin/env python
                  from tkinter import *
                  from tkinter import ttk
                  import tkinter.messagebox
                  import os
                  import urllib.request
                  
                  sourcecode = ""
                  
                  def geturl(*args): #accept an argument for return
                      path = urlname.get()
                      if path == "":
                             tkinter.messagebox.showinfo("error", "please enter a url")
                      if "http://" not in path:
                          http ="http://"
                          path = http + path
                      with urllib.request.urlopen(path) as url:
                          sourcecode = url.read()
                          global storecode
                          storecode = sourcecode
                          string2 = "source code copied from : " + path
                          tkinter.messagebox.showinfo("copied", string2)
                          Text.delete(1.0, END)#delete currently in text box
                          Text.insert(tkinter.END,storecode)
                          return
                  
                              #find white spaces in source code
                  def count_white_space():
                      path = urlname.get()
                      if path == "":
                             tkinter.messagebox.showinfo("error", "please enter a url")
                  
                      if "http://" not in path:
                          http ="http://"
                          path = http + path
                          with urllib.request.urlopen(path) as url:
                              sourcecode = url.readlines()
                              global storecode
                              storecode = sourcecode
                              whitespace = 0
                              for item in str(sourcecode):
                                  if item == ' ':
                                      whitespace +=1
                              string1 = "There are " + str(whitespace) + " white spaces in: " + path
                              tkinter.messagebox.showinfo("whitespace", string1)
                  
                  
                  app = Tk()
                  app.title(" text editor")
                  
                  content = ttk.Frame(app, padding=(3,3,12,12))
                  content.grid(column=0, row=0,sticky=(N, S, E, W))
                  
                  #creat label
                  labeltext = StringVar()
                  labeltext.set("enter url:")
                  label1 = ttk.Label(content, textvariable=labeltext).grid(column=0, row=1, columnspan=1, rowspan=1, sticky=(N, W), padx=5)
                  
                  #create text box
                  urlname = StringVar()# text being enterd in tht text box is stored in urlname
                  
                  urlname_entry = ttk.Entry(content, textvariable=urlname)
                  urlname_entry.grid(column=1, row=1, columnspan=3,rowspan=5, sticky=(N) )
                  #focus in the text box so user dont have to click on
                  urlname_entry.focus()
                  #create button
                  
                  button1 = ttk.Button(content,text="get source", command=geturl)
                  button2 = ttk.Button(content,text="count white spaces", command=count_white_space)
                  button1.grid(column=3,row=0, columnspan=1, rowspan=2, sticky=(N,W))
                  button2.grid(column=4,row=0,columnspan=2, rowspan=2, sticky=(N,W))
                  
                  
                  scroll = tkinter.Scrollbar(content,borderwidth=2)
                  Text = tkinter.Text(content,wrap=CHAR, width=30, height=20)
                  scrollh = tkinter.Scrollbar(content,borderwidth=2, orient=HORIZONTAL)
                  
                  
                  scrollh.config(command=Text.xview)
                  Text.config(xscrollcommand=scrollh.set)
                  
                  scroll.config(command=Text.yview)
                  Text.config(yscrollcommand=scroll.set)
                  
                  Text.grid(row=2, column=1,columnspan=1, rowspan=3, sticky=(N))
                  scroll.grid(row=3,column=3)
                  scrollh.grid(row=6, rowspan=3, column=1)
                  
                  app.columnconfigure(0, weight=1)
                  app.rowconfigure(0, weight=1)
                  content.columnconfigure(0, weight=3)
                  content.columnconfigure(1, weight=3)
                  content.columnconfigure(2, weight=3)
                  content.columnconfigure(3, weight=1)
                  content.columnconfigure(4, weight=1)
                  content.rowconfigure(1, weight=1)
                  
                  
                  
                  
                  #text = Text(app, width=80,height=40, wrap='none').grid(row=2, column=2)
                  
                  
                  
                  
                  
                  
                  #adds spacing between widgets
                  for child in app.winfo_children(): child.grid_configure(padx=5, pady=5)
                  for child in content.winfo_children(): child.grid_configure(padx=5, pady=5)
                  
                  app.bind('<Return>',geturl) #enter can also be hit
                  
                  
                  
                  app.mainloop()

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Try this:
                    Code:
                    Text.config(yscrollcommand=scroll.set, wrap=tkinter.NONE,)
                     
                    Text.grid(row=2, column=1,columnspan=1, rowspan=3, sticky=(N))
                    scroll.grid(row=2,column=2, sticky='ns', rowspan=3)
                    scrollh.grid(row=6, rowspan=1, column=1, sticky='ew')
                    Last edited by bvdet; Feb 14 '12, 07:45 PM. Reason: Tkinter is lower case in Python 3

                    Comment

                    • moroccanplaya
                      New Member
                      • Jan 2011
                      • 80

                      #11
                      it work i really appreciate your help, can you explain where i went wrong, so the scroll.grid have to include sticky and rowspan

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        The horizontal scrollbar will never work unless the text window needs to expand. It will only need to expand if wrap=tkinter.NO NE

                        Comment

                        Working...