def graphics ():
window = tkinter.Tk()
window.geometry("800x800") #You enter the window dimensions for
canvas = tkinter.Canvas(window, bg = 'white', width=800, height=800)
#Code for creating graphical shapes
# note for me: im going to make a graphical drawing that draws a the sizes of the cubes user wants ! (so if there's a 2x2 cube, 3x3, and 4x4, ima draw those (2d only)
user_size = input("What size cube would you like to have drawn? (3x3, 2x2, ...): ")
user_size.split()
cube_size = int(user_size[0])
cube_size = 1
counter = 0
cube_y = 800
cube_y2 = 700
cube_x = 100
cube_x2 = 200
counter = 1
e = 'red'
while (counter != cube_size):
y = canvas.create_polygon(100,cube_y,200,cube_y,200,cube_y2,100,cube_y2, fill = e, outline = 'black', width = 7)
cube_y = cube_y - 100
cube_y2 = cube_y2 - 100
print(counter)
counter = 1
#Flips the shapes from memory onto the monitor
canvas.pack()
window.mainloop()
正如標題所說,我想為學校制作一個繪制魔方的程式。本質上,用戶輸入立方體的大小(3x3,4x4,...),程式讀取輸入大小的第一個數字。因此,如果用戶寫入 3x3,程式將在開頭讀取數字“3”。
從那里,程式將運行y = canvas.create_polygon(100,cube_y,200,cube_y,200,cube_y2,100,cube_y2, fill = e, outline = 'black', ,創建一個 100x100 像素寬的紅色塊。對于回圈運行的次數,塊將上升 100 像素(它將上升 y 軸)。例如,如果用戶輸入 3x3,y 代碼塊將被制作 3 次,每次向上 100 px。
如果我輸入 3x3,這就是它的樣子
我需要幫助的是處理 x 軸。我已經設法讓程式在垂直軸上繪制立方體,但我無法在水平軸上進行管理。我的計劃是讓代碼y = canvas.create_polygon(100,cube_y,200,cube_y,200,cube_y2,100,cube_y2, fill = e, outline = 'black'運行與計數器一樣多的次數,但每次,多邊形的 x 點都會向右移動 100 像素。
所以對于前。如果我要繪制 4x4 立方體,程式將首先使用我之前提到的 y = canvas.create_polygon() 代碼繪制 4 個向上/垂直/y 軸的立方體,然后它會繪制相同的形狀,但這次整個代碼將在 x 軸上水平移動 100 px。最終產品應該看起來像這樣 (請原諒我在 google jamboard 上使用滑鼠繪制的爛圖)
如果有人對如何做到這一點有任何想法,請告訴我。并提前為文字墻道歉!謝謝!
uj5u.com熱心網友回復:
這是我寫的代碼,這是為了給你一個開始,它并不完美,但你可以根據需要調整立方體的大小和視窗。
我實作了一個回圈,它在列和行上回圈,設定為立方體大小。如果立方體大小設定為 3,則回圈將從 1 變為 3 并繪制每個形狀。使用 繪制形狀canvas.create_rectangle(tileSize * col, tileSize * row, tileSize * col tileSize, tileSize * row tileSize, fill='red')。
tileSize * col=50*1, 50*2, 50*3 == 50, 100, 150
也是一樣的tileSize * row
第二個x和y是上面的 x 和 y tileSize,使每個矩形為 50x50 像素。您可以根據自己的喜好調整大小和位置。
調整這部分:canvas.create_rectangle( tileSize * col, tileSize * row, tileSize * col tileSize, tileSize * row tileSize, fill='red') 以更改立方體開始繪制的位置。
調整這部分:canvas.create_rectangle( tileSize* col, tileSize* row, tileSize * col tileSize, tileSize * row tileSize, fill='red') 為每個正方形的大小。
from tkinter import *
def graphics():
window = Tk()
window.geometry("800x800") # You enter the window dimensions for
# tile/window sizes
tileSize = 50
x = tileSize * 5
y = tileSize * 5
canvas = Canvas(window, bg='white', width=x, height=y)
cols = int(input('What size cube do you want?: '))
rows = cols
for col in range(cols):
for row in range(rows):
canvas.create_rectangle(tileSize * col, tileSize * row, tileSize * col tileSize, tileSize * row tileSize, fill='red')
# Flips the shapes from memory onto the monitor
canvas.pack()
window.mainloop()
graphics()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/475492.html
標籤:Python tkinter 动画片 tkinter-entry
