2017-05-29 121 views
0

幾周來,我嘗試讓我的應用程序滾動條。但是與TKinter模塊有衝突,我不知道如何解決它。沒有我的模塊,它是滾動條,但不是與他們。你可以幫我請一下這個例子嗎?Python - 滾動條如何處理標籤,單選按鈕,按鈕?

Lovespock

Ps。請好,我是一個半新手。 :/:p PP。我沒有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() 
+0

什麼錯誤(衝突)消息? – frankyjuang

+0

沒有錯誤消息,應用程序不會執行任何操作。她沒有開始,但她沒有滾動條。 – LoveSpock

+0

你能提供完整的代碼嗎? – frankyjuang

回答

0

要滾動Canvas內部小部件,而不是使用通常的packgridplace方法來顯示它們,你需要使用Canvascreate_window方法。該方法的語法是 canvas.create_window(x, y, window=widget, **kw)Here你可以得到有關關鍵字參數的細節。

例子:

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

好的,這個語法是可能的,謝謝!還有一個問題,你使用「爲我的範圍」,但我沒有一個標籤,但有10個不同。我認爲我不能和類一起工作,但是如何在不編寫相同代碼10次的情況下使用這0個不同的標籤? – LoveSpock

+0

如果唯一不同的是標籤的文本,您可以創建一個字符串列表'label_text',並用'label ='替換'label = tk.Label(canvas,text ='label%i'%i)'' tk.Label(canvas,text = label_text [i])'。 –