0

For weeks I try to get my application scrollbar. But there is a conflict with the TKinter-modules and I don't know how to fix it. Without my modules it is scrollbar but not with them. Can you help me please with this examples out of my application?

Lovespock

Ps. Please be nice, I am a half-newbie. :/ :p PPs. My code without textdefinitions and all radiobuttons

from __future__ import print_function
from Tkinter import *
import Tkinter as tk
from result import *

master = tk.Tk()

frame=Frame(master,width=300,height=300)
frame.pack
canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))

vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)

def sumlist():
    sum = (vallist_a[-1]) + (vallist_b[-1]) + (vallist_c[-1]) + (vallist_d[-1]) + (vallist_e[-1]) + (vallist_f[-1]) + (vallist_g[-1]) + (vallist_h[-1]) + (vallist_i[-1])
    print (sum)

def create_window(): #Definion und Festlegung neues Fenster
    toplevel = Toplevel()
    toplevel.title('result')
    toplevel.geometry('1500x1000')
    toplevel.focus_set()
    sum = (vallist_a[-1]) + (vallist_b[-1]) + (vallist_c[-1]) + (vallist_d[-1]) + (vallist_e[-1]) + (vallist_f[-1]) + (vallist_g[-1]) + (vallist_h[-1]) + (vallist_i[-1])
    if 9<= sum <= 14:
        msg = Message(toplevel, text = result1)
        msg.config(bg='lightgreen', font=('times', 24, 'italic'))
        msg.pack()
    if 15<= sum <= 21:
        msg = Message(toplevel, text = result2)
        msg.config(bg='lightgreen', font=('times', 24, 'italic'))
        msg.pack()
    if 22<= sum <= 28:
        msg = Message(toplevel, text = result3)
        msg.config(bg='lightgreen', font=('times', 24, 'italic'))
        msg.pack()
    if 29<= sum <= 36:
        msg = Message(toplevel, text = result4)
        msg.config(bg='lightgreen', font=('times', 24, 'italic'))
        msg.pack()


def ShowChoice(text, v, testlist):
    print(v.get())
    testlist.append(v.get())

vallist_a = []
vallist_b = []
vallist_c = []
vallist_d = []
vallist_e = []
vallist_f = []
vallist_g = []
vallist_h = []
vallist_i = []


hello = [
    (1),
    (2),
    (3),
    (4),
]

officer = [
    (1),
    (2),
    (3),
    (4),
]

borg = [
    (1),
    (2),
    (3),
    (4),
]

vulcans = [
    (1),
    (2),
    (3),
    (4),
]

home = [
    (1),
    (2),
    (3),
    (4),
]

crew = [
    (1),
    (2),
    (3),
    (4),
]

important = [
    (1),
    (2),
    (3),
    (4),
]

take = [
   (1),
   (2),
   (3),
   (4),
]

bye = [
    (1),
    (2),
    (3),
    (4),
]

tk.Label(canvas, text='Which Captain are you?', font=("Helvetica", 30), fg = 'red').pack()

varhello = tk.IntVar()

for i, val in enumerate(hello):
    tk.Radiobutton(canvas, text=text[i], variable=varhello, value=val,
    command=lambda t=text, a=varhello: ShowChoice(t, a, vallist_a)).pack(anchor=tk.N)


Button(canvas, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)


canvas.config(width=300,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)

master.mainloop()
LoveSpock
  • 15
  • 3
  • 7
  • what is the error (conflict) message? – YLJ May 29 '17 at 13:39
  • There is no error message, the application does not do anything. She does not start but she does without scrollbar. – LoveSpock May 29 '17 at 14:09
  • can you provide the complete code? – YLJ May 29 '17 at 14:12
  • Okay, but it is long. Wait a minute. – LoveSpock May 29 '17 at 14:16
  • 2
    You are using both grid and pack on same parent(`master`). Choose one and stick to it. – Lafexlos May 29 '17 at 14:18
  • Related: [python pack() and grid() methods together](https://stackoverflow.com/questions/17267140/python-pack-and-grid-methods-together) – Lafexlos May 29 '17 at 14:20
  • frankyjuang: My code without text and all Radiobuttons, principal they have the same syntax. Lafexlos: Thx, I try! :) – LoveSpock May 29 '17 at 14:26
  • Okay. I have chance .grid in .pack and it I tkink I had the wrong parent, so I had change it in canvas. No I can open my application and see the scrollbar frame but it does not scroll. – LoveSpock May 29 '17 at 14:39
  • To scroll the widgets inside your canvas, you should not pack/grid them, but use instead the `canvas.create_window(x, y, window=widget)` method. – j_4321 May 29 '17 at 15:17
  • Can you give an example please? – LoveSpock May 29 '17 at 16:46
  • I added an answer with an example. By the way, people who comment a post without being the author don't receive notification when a new comment is posted, so when you reply to someone's comment, you should use @username at the beginning of the comment so that he/she is notified. – j_4321 May 30 '17 at 07:38

1 Answers1

1

To scroll widgets inside a Canvas, instead of using the usual pack, grid or place methods to display them, you need to use the create_window method of the Canvas. The syntax of the method is canvas.create_window(x, y, window=widget, **kw). Here you can get details about the keyword arguments.

Example:

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root)
scrolly = tk.Scrollbar(root, orient='vertical', command=canvas.yview)

# display labels in the canvas
for i in range(10):
    label = tk.Label(canvas, text='label %i' % i)
    canvas.create_window(0, i*50, anchor='nw', window=label, height=50)

canvas.configure(scrollregion=canvas.bbox('all'), yscrollcommand=scrolly.set)

canvas.pack(fill='both', expand=True, side='left')
scrolly.pack(fill='y', side='right')

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Okay, this syntax is possible, thank you! One more question, you use "for i in range" but I have not one label but 10 different. I think I can't work with classes but how I can use this 0 deifferent labels without writing the same code 10 times? – LoveSpock May 30 '17 at 11:05
  • If the only thing that varies is the text of the label, you can create a list of strings `label_text` and replace `label = tk.Label(canvas, text='label %i' % i)` by `label = tk.Label(canvas, text=label_text[i])`. – j_4321 May 30 '17 at 11:14