Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with Python Tkinter: Frames within a Canvas
    primarykey
    data
    text
    <p>I'm trying to set up a scrollable series of Frames, so I've nested them into a Canvas. Below, I've included some sample code that demonstrates the problem.</p> <p>However, I have three issues:</p> <ol> <li>Frame <code>c</code> doesn't seem to expand horizontally to fill the canvas.</li> <li>If I use multiple frames <code>c</code>, the canvas scrolls, but if I try to nest my <code>FrameRow</code>s into a single frame <code>c</code>, the canvas no longer scrolls.</li> <li>With multiple frames <code>c</code>, the last of the <code>FrameRow</code>s is cut off.</li> </ol> <p>Buttons "Left" and "Right" demonstrate the behavior I get when the buttons are not nested in a Canvas.</p> <p>I'd prefer using the single Frame <code>c</code> and nesting all <code>FrameRows</code> in that, but I would need the scrolling to work properly for that.</p> <pre><code>import * from Tkinter import FrameRow root = Tk() root.title("Testing") containerFrame = Frame(root) containerFrame.config(bg="red") containerFrame.pack(side=TOP, fill=BOTH, expand=1) # Frame showing proper behavior a = Frame(containerFrame, bg="black") a.pack(side=TOP, fill=X, expand=0) btnLeft = Button(a) btnLeft.config(text="LEFT") btnLeft.pack(side=LEFT, fill=X, expand=1) btnRight = Button(a) btnRight.config(text="RIGHT") btnRight.pack(side=RIGHT, fill=X, expand=0) # Canvas canvas = Canvas(containerFrame) scrollbar = Scrollbar(containerFrame, orient=VERTICAL) scrollbar.config(command=canvas.yview) canvas.config(bg="blue", yscrollcommand=scrollbar.set) scrollbar.pack(side=RIGHT, fill=Y) canvas.pack(side=LEFT, fill=BOTH, expand=1) # Multiple Frames within Canvas frameRow = range(30) c = range(30) for r in xrange(0,30): c[r] = Frame(canvas) c[r].config(bg="green") frameRow[r] = FrameRow.FrameRow() frameRow[r].setFrame(c[r]) frameRow[r].setRow(r) frameRow[r].createRow() c[r].pack(side=TOP, fill=X, expand=1) canvas.create_window(0, (r+1)*30, window=c[r], anchor="nw") canvas.config(scrollregion = canvas.bbox(ALL)) # OR # Single Frame within Canvas ##c = Frame(canvas) ##c.config(bg="green") ## ##frameRow = range(30) ##for r in xrange(0,30): ## frameRow[r] = FrameRow.FrameRow() ## frameRow[r].setFrame(c) ## frameRow[r].setRow(r) ## frameRow[r].createRow() ## ##c.pack(side=TOP, fill=X, expand=1) ##canvas.create_window(0, 0, window=c, anchor="nw") root.mainloop() </code></pre> <p>The <code>FrameRow</code> class is defined as:</p> <pre><code>from Tkinter import * class FrameRow(): _frame = "Default" _row = 0 def setFrame(self, frame): self._frame = frame def setRow(self, row): self._row = row def createRow(self): self.frameRow = Frame(self._frame) self.frameRow.config(bg="yellow") self.frameRow.pack(side=TOP, fill=X, expand=1) self.btn1 = Button(self.frameRow) self.btn1.config(text="Button "+str(self._row)+"A") self.btn1.pack(side=LEFT, fill=X, expand=1) self.btn2 = Button(self.frameRow) self.btn2.config(text="Button "+str(self._row)+"B") self.btn2.pack(side=RIGHT, anchor=E, fill=X, expand=0) </code></pre> <p>Is there something I'm missing in getting Frame(s) <code>c</code> to expand to the width of the canvas?</p> <p>Am I doing something wrong with the scrolling and the single Frame <code>c</code>?</p> <p>Are there any decent tutorials or examples that show how I should be doing this?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload