简体   繁体   English

Python-滚动条如何与标签,单选按钮,按钮一起使用?

[英]Python - How Scrollbar does work with Labels, radiobuttons, Buttons?

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. 但是TKinter模块存在冲突,我不知道如何解决。 Without my modules it is scrollbar but not with them. 没有我的模块,它是滚动条,但没有滚动条。 Can you help me please with this examples out of my application? 您能否在我的应用程序中帮我解决这个例子?

Lovespock Lovespock

Ps. PS。 Please be nice, I am a half-newbie. 请保持友好,我是新手。 :/ :p PPs. :/:p PP。 My code without textdefinitions and all radiobuttons 我的代码没有textdefinitions和所有单选按钮

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()

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 . 要滚动Canvas窗口小部件,而不是使用通常的packgridplace方法来显示它们,您需要使用Canvascreate_window方法。 The syntax of the method is canvas.create_window(x, y, window=widget, **kw) . 该方法的语法为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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM