2017-04-23 66 views
1

描画プログラムをコーディングしていて、キャンバスの背景色を変更するオプションを作成しようとしていますが、何らかの理由で機能しません。エラーメッセージは表示されず、ラインの色を変更するために同じコードを使用していたので、なぜ機能しないのかわかりません。Python tkinterウィンドウの背景/キャンバスの色が変更されない

P.S.私は非常にPythonに新しいです。

コード:

from tkinter import * 

# canvas specifications/variables 
canvas_height = 400 
canvas_width = 600 
p1_canvas_colour = "white" 
p1_canvas_colour_number = 1 

#p1 (what you draw with) specifications/variables 
p1_x = canvas_width/2 
p1_y = canvas_height 
p1_colour = "black" 
p1_colour_change = 1 
line_width = 1 
line_length = 1 

#p1 controls 
def p1_move_N(event): 
    global p1_y 
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=p1_colour) 
    p1_y = p1_y - line_length 

def p1_move_S(event): 
    global p1_y 
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y+line_length), width=line_width, fill=p1_colour) 
    p1_y = p1_y + line_length 

def p1_move_E(event): 
    global p1_x 
    canvas.create_line(p1_x, p1_y, (p1_x+line_length), p1_y, width=line_width, fill=p1_colour) 
    p1_x = p1_x + line_length 

def p1_move_W(event): 
    global p1_x 
    canvas.create_line(p1_x, p1_y, (p1_x-line_length), p1_y, width=line_width, fill=p1_colour) 
    p1_x = p1_x - line_length 

def erase_all(event): 
    canvas.delete(ALL) 

#the p1 colour change 
def p1_line_colour_change(event): 
    global p1_colour 
    global p1_colour_change 
    p1_colour_change += 1 
    if p1_colour_change == 1: 
     p1_colour = ("black") 
    if p1_colour_change == 2: 
     p1_colour = ("white") 
    if p1_colour_change == 3: 
     p1_colour = ("red") 
    if p1_colour_change == 4: 
     p1_colour = ("orange") 
    if p1_colour_change == 5: 
     p1_colour = ("yellow") 
    if p1_colour_change == 6: 
     p1_colour = ("green") 
    if p1_colour_change == 7: 
     p1_colour = ("blue") 
    if p1_colour_change == 8: 
     p1_colour = ("purple") 
    if p1_colour_change == 9: 
     p1_colour = ("pink") 
    if p1_colour_change == 10: 
     p1_colour = ("brown") 
    if p1_colour_change == 11: 
     p1_colour_change = 1 
     p1_colour = ("black") 
#the canvas colour change 
def p1_canvas_colour_change(event): 
    global p1_canvas_colour 
    global p1_canvas_colour_number 
    p1_canvas_colour_number += 1 
    if p1_canvas_colour_number == 1: 
     p1_canvas_colour = ("white") 
    if p1_canvas_colour_number == 2: 
     p1_canvas_colour = ("black") 
    if p1_canvas_colour_number == 3: 
     p1_canvas_colour = ("red") 
    if p1_canvas_colour_number == 4: 
     p1_canvas_colour = ("orange") 
    if p1_canvas_colour_number == 5: 
     p1_canvas_colour = ("yellow") 
    if p1_canvas_colour_number == 6: 
     p1_canvas_colour = ("green") 
    if p1_canvas_colour_number == 7: 
     p1_canvas_colour = ("blue") 
    if p1_canvas_colour_number == 8: 
     p1_canvas_colour = ("purple") 
    if p1_canvas_colour_number == 9: 
     p1_canvas_colour = ("pink") 
    if p1_canvas_colour_number == 10: 
     p1_canvas_colour = ("brown") 
    if p1_canvas_colour_number == 11: 
     p1_canvas_colour_number = 1 
     p1_canvas_colour = ("white") 
#the window/canvas 
window = Tk() 
window.title("Drawing") 
canvas = Canvas(bg=p1_canvas_colour, height=canvas_height, width=canvas_width, highlightthickness=0) 
canvas.pack() 

#binding the functions to keys 
window.bind("<Up>", p1_move_N) 
window.bind("<Down>", p1_move_S) 
window.bind("<Left>", p1_move_W) 
window.bind("<Right>", p1_move_E) 
window.bind("<BackSpace>", erase_all) 
window.bind("<KP_Enter>", p1_line_colour_change) 
window.bind("<Shift_R>", p1_canvas_colour_change) 

window.mainloop() 

答えて

2

変更を適用するためにあなたのキャンバスを再設定する必要があり、 はあなたの「p1_canvas_colour_change」メソッドの最後に次の行を追加します。

canvas.configure(background=p1_canvas_colour) 
+0

ありがとうございました、私はあなたに最高の答えを与えました。 – BloodPanther

関連する問題