By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Why isn't the canvas scrolling when using the scrollbars? And how to stop the canvas

P: 1
I'm a newbie in python-tkinter. And I have a problem that when i put widgets inside my canvas, the widgets bypasses the canvas size. But when I scroll, the canvas would not move. On top of that, the canvas auto re-sizes when I add more widgets, isn't it not suppose to re-size 'coz I already added a scrollbar?

Here's my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. from tkinter import *
  3. from tkinter import ttk
  4. import mysql.connector
  5.  
  6. class CanvasScroll:
  7.  
  8.     def on_resize(self,event):
  9.         self.canvas.config(width = 1185, height = 530)
  10.  
  11.     def add_crew(self):
  12.         crewFrame = CrewFrame()
  13.  
  14.  
  15.     def callback_list(self):
  16.         index = self.notebook.index(self.notebook.select()) + 1
  17.         self.tmpframe = Try2(self.contentframe, index)
  18.  
  19.  
  20.     def __init__(self, master):
  21.         self.canvas = Canvas(master, width = 1185, height = 530, scrollregion = (0, 0, 1216, 700), bg = 'white', confine = True)
  22.         self.canvas.grid(row = 2, column = 0, sticky = 'news')
  23.  
  24.         self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
  25.         self.xscroll.grid(row = 3, column = 0, sticky = 'we')
  26.  
  27.         self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
  28.         self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
  29.  
  30.         self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
  31.         self.canvas.bind("<Configure>", self.on_resize)
  32.  
  33.  
  34.         self.option = ttk.Frame(master, height = 150, width = 1206)
  35.         self.option.grid(row = 0, column = 0, sticky = 'we', columnspan = 5)
  36.  
  37.         self.addicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\add.gif').subsample(2,2)
  38.         self.btnAdd = ttk.Button(self.option, image = self.addicon, text = 'Add Crew', compound = TOP, command = self.add_crew)
  39.         self.btnAdd.grid(row = 0, column = 0, padx = 50, pady = 5)
  40.  
  41.         self.updateicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\update.gif').subsample(2,2)
  42.         self.btnUpdate = ttk.Button(self.option, image = self.updateicon, text = 'Update Crew', compound = TOP)
  43.         self.btnUpdate.grid(row = 0, column = 1, padx = 50)
  44.  
  45.         self.deleteicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\delete.gif').subsample(2,2)
  46.         self.btnDelete = ttk.Button(self.option, image = self.deleteicon, text = 'Delete Crew', compound = TOP)
  47.         self.btnDelete.grid(row = 0, column = 2, padx = 50)
  48.  
  49.         self.reloadicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\Refresh.png').subsample(7,7)
  50.         self.reloadbtn = ttk.Button(self.option, image = self.reloadicon, command = self.callback_list, text = 'Load List', compound = TOP)
  51.         self.reloadbtn.grid(row = 0, column = 3, padx = 50)
  52.  
  53.         self.tabframe = ttk.Frame(master, height = 20)
  54.         self.tabframe.grid(row = 1, sticky = 'we')
  55.  
  56.         self.notebook = ttk.Notebook(self.tabframe)
  57.         self.notebook.grid(row = 0, column = 0, sticky = 'we')
  58.  
  59.         db = mysql.connector.connect(user = 'root', password = '', host = 'localhost', database = 'fat2x_payroll')
  60.         cursor = db.cursor()
  61.  
  62.         ships = ("SELECT * from tbl_shiplist")
  63.  
  64.         try:
  65.             cursor.execute(ships)
  66.             result = cursor.fetchall()
  67.             self.tab = {}
  68.             for row in result:
  69.                 self.tab[row[0]] = {
  70.                     "shipname": ttk.Frame(self.notebook)
  71.                     }
  72.                 self.notebook.add(self.tab[row[0]]["shipname"], text = row[1])
  73.                 db.close()
  74.  
  75.         except:
  76.             messagebox.showinfo("alert", "ship list error")
  77.  
  78.         self.canvas.label = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 0)
  79.         self.canvas.label1 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 1)
  80.         self.canvas.label2 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 2)
  81.         self.canvas.label3 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 3)
  82.         self.canvas.label4 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 4)
  83.         self.canvas.label5 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 5)
  84.         self.canvas.label6 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 6)
  85.         self.canvas.label7 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 7)
  86.         self.canvas.label8 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 8)
  87.  
  88.  
  89.  
  90.  
  91.  
  92. def start():
  93.     master = Tk()
  94.     master.geometry("1206x690+10+10")
  95.     master.resizable(0,0)
  96.     master.title("Crew Panel")
  97.     canvasScroll = CanvasScroll(master)
  98.  
  99. if __name__ == "__main__":
  100.     start()
  101.  
  102.  
Can someone please help me! :(
Jan 20 '15 #1

✓ answered by dwblas

A mainloop statement is required to start Tkinter. Other than that, this version, with unnecessary code stripped out, works fine for me. Also take note of the master.resizable function added to the code, and remember that scrollbars only work within the scrollregion size.

Expand|Select|Wrap|Line Numbers
  1. class CanvasScroll:
  2.      def __init__(self, master):
  3.          ## scrollregion increased but all the widgets
  4.          ## still don't fit -- see what prints (Python 2.X)
  5.          self.canvas = Canvas(master, width = 1185, height = 530,
  6.                               scrollregion = (0, 0, 1800, 700),
  7.                               bg = 'white', confine = True)
  8.          self.canvas.grid(row = 2, column = 0, sticky = 'news')
  9.  
  10.          self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
  11.          self.xscroll.grid(row = 3, column = 0, sticky = 'we')
  12.  
  13.          self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
  14.          self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
  15.  
  16.          self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
  17.  
  18.          for ctr in range(10):
  19.              self.canvas.create_text(150, 50+(ctr*100),
  20.                                      text='test-'+str(ctr+1))
  21.  
  22.          ## wait one second
  23.          master.after(1000, self.add_more)
  24.  
  25.      def add_more(self):
  26.          scroll_width=1800
  27.          for ctr in range(10):
  28.              x=300+(ctr*200)
  29.              dif=scroll_width-x
  30.              lit=""
  31.              if dif < 0:
  32.                  lit="Not Displayed"
  33.              print ctr+1, x, dif, lit
  34.  
  35.              self.canvas.create_text(x, 50,
  36.                              text='width-'+str(ctr+1))
  37.  
  38. master = Tk()
  39. master.geometry("1206x690+10+10")
  40. master.title("Crew Panel")
  41. master.resizable(width=FALSE, height=FALSE)
  42. canvasScroll = CanvasScroll(master)
  43. master.mainloop() 

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 618
A mainloop statement is required to start Tkinter. Other than that, this version, with unnecessary code stripped out, works fine for me. Also take note of the master.resizable function added to the code, and remember that scrollbars only work within the scrollregion size.

Expand|Select|Wrap|Line Numbers
  1. class CanvasScroll:
  2.      def __init__(self, master):
  3.          ## scrollregion increased but all the widgets
  4.          ## still don't fit -- see what prints (Python 2.X)
  5.          self.canvas = Canvas(master, width = 1185, height = 530,
  6.                               scrollregion = (0, 0, 1800, 700),
  7.                               bg = 'white', confine = True)
  8.          self.canvas.grid(row = 2, column = 0, sticky = 'news')
  9.  
  10.          self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
  11.          self.xscroll.grid(row = 3, column = 0, sticky = 'we')
  12.  
  13.          self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
  14.          self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
  15.  
  16.          self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
  17.  
  18.          for ctr in range(10):
  19.              self.canvas.create_text(150, 50+(ctr*100),
  20.                                      text='test-'+str(ctr+1))
  21.  
  22.          ## wait one second
  23.          master.after(1000, self.add_more)
  24.  
  25.      def add_more(self):
  26.          scroll_width=1800
  27.          for ctr in range(10):
  28.              x=300+(ctr*200)
  29.              dif=scroll_width-x
  30.              lit=""
  31.              if dif < 0:
  32.                  lit="Not Displayed"
  33.              print ctr+1, x, dif, lit
  34.  
  35.              self.canvas.create_text(x, 50,
  36.                              text='width-'+str(ctr+1))
  37.  
  38. master = Tk()
  39. master.geometry("1206x690+10+10")
  40. master.title("Crew Panel")
  41. master.resizable(width=FALSE, height=FALSE)
  42. canvasScroll = CanvasScroll(master)
  43. master.mainloop() 
Jan 20 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.