Hướng dẫn long python code copy and paste - sao chép và dán mã python dài

Vấn đề chính với mã của bạn là bạn tạo một PhotoImage mới cho mỗi pixel! Thay vào đó, hãy tạo PhotoImage một lần và chỉ cần thêm các pixel trong đôi-____ 6 vòng.

def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)

    picture = PhotoImage(width=(x2-x1), height=(y2-y1))
    for x in range(x1, x2):
        for y in range(y1, y2):
            r, g, b = photo.get(x, y)
            picture.put("#%02x%02x%02x" % (r, g, b), (x-x1, y-y1))
    picture.write('new_image.gif', format='gif')

Ngoài ra, dòng tuple(map(int, value.split(" "))) trong hàm getRGB của bạn là sai, vì value đã là bộ xử lý bạn muốn tạo, không phải là một chuỗi.1) như bạn có thể thấy, tôi chỉ 'phần' phần đó trực tiếp vào hàm

def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
0. Một vấn đề khác là bạn đã viết các pixel đã sao chép thành
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
1 và
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
2, nhưng bạn phải viết chúng lên
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
3 và
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
4 thay thế.

CẬP NHẬT 1: 1) Có vẻ như giá trị trả lại của

def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
5 phụ thuộc vào phiên bản Python/Tkinter mà bạn đang sử dụng. Trong một số phiên bản, nó trả về một tuple, như
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
6 và trong các phiên bản khác, một chuỗi, như
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
7.
1) It seems like the return value of
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
5 depends on the version of Python/Tkinter you are using. In some versions, it returns a tuple, like
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
6, and in others, a string, like
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
7.

CẬP NHẬT 2: Như được chỉ ra bởi @oblivion, trên thực tế, bạn có thể chỉ sử dụng tham số

def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
8 của
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
9 để chỉ định vùng của hình ảnh sẽ được lưu vào tệp. Với điều này, hàm
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
0 có thể được đơn giản hóa như
As pointed out by @Oblivion, you can in fact just use the
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
8 parameter of
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
9 to specify the region of the picture to be saved to file. With this, the
def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
0 function can be simplified as

def box(event):
    yaxis(event)
    canvas.create_rectangle(x1, y1, x2, y2)
    photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107

def print_board(board):

	print "The board look like this: \n"

	for i in range(3):
		print " ",
		for j in range(3):
			if board[i*3+j] == 1:
				print 'X',
			elif board[i*3+j] == 0:
				print 'O',	
			elif board[i*3+j] != -1:
				print board[i*3+j]-1,
			else:
				print ' ',
			
			if j != 2:
				print " | ",
		print
		
		if i != 2:
			print "-----------------"
		else: 
			print 
			
def print_instruction():
	print "Please use the following cell numbers to make your move"
	print_board([2,3,4,5,6,7,8,9,10])


def get_input(turn):

	valid = False
	while not valid:
		try:
			user = raw_input("Where would you like to place " + turn + " (1-9)? ")
			user = int(user)
			if user >= 1 and user <= 9:
				return user-1
			else:
				print "That is not a valid move! Please try again.\n"
				print_instruction()
		except Exception as e:
			print user + " is not a valid move! Please try again.\n"
		
def check_win(board):
	win_cond = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7))
	for each in win_cond:
		try:
			if board[each[0]-1] == board[each[1]-1] and board[each[1]-1] == board[each[2]-1]:
				return board[each[0]-1]
		except:
			pass
	return -1

def quit_game(board,msg):
	print_board(board)
	print msg
	quit()

def main():
	
	# setup game
	# alternate turns
	# check if win or end
	# quit and show the board
	
	print_instruction()

	board = []
	for i in range(9):
		board.append(-1)

	win = False
	move = 0
	while not win:

		# print board
		print_board(board)
		print "Turn number " + str(move+1)
		if move % 2 == 0:
			turn = 'X'
		else:
			turn = 'O'

		# get user input
		user = get_input(turn)
		while board[user] != -1:
			print "Invalid move! Cell already taken. Please try again.\n"
			user = get_input(turn)
		board[user] = 1 if turn == 'X' else 0

		# advance move and check for end game
		move += 1
		if move > 4:
			winner = check_win(board)
			if winner != -1:
				out = "The winner is " 
				out += "X" if winner == 1 else "O" 
				out += " :)"
				quit_game(board,out)
			elif move == 9:
				quit_game(board,"No winner :(")

if __name__ == "__main__":
	main()
	

Tôi có thể sao chép mã python Paste không?

Để sao chép văn bản, chỉ cần chọn nó và nhấn Ctrl-C (Command-C trên Mac).Nếu điểm nổi bật đánh dấu lựa chọn biến mất, điều đó là bình thường và nó có nghĩa là nó đã hoạt động.Để dán, sử dụng Ctrl-V (Command-V trên máy Mac).. If the highlight marking the selection disappears, that's normal and it means it's worked. To paste, use Ctrl-V (Command-V on a Mac).

Làm thế nào để bạn sao chép mã python trong python?

Cách sao chép một tệp bằng Python (bao gồm các ví dụ)..
Bước 1: Chụp đường dẫn ban đầu.Để bắt đầu, hãy chụp đường dẫn nơi tệp của bạn hiện đang được lưu trữ.....
Bước 2: Chụp đường dẫn đích.Tiếp theo, chụp đường dẫn đích nơi bạn muốn sao chép tệp.....
Bước 3: Sao chép tệp trong Python bằng SHOWIL.copyfile ..

Mã Python phức tạp nhất là gì?

Mã Python phức tạp nhất là gì?OpenStack (Kiến trúc đám mây được NASA và CERN thông qua) là mã Python phức tạp nhất từ trước đến nay: nó đếm 2'400'000 dòng.Openstack (the cloud architecture adopted by NASA and CERN) is reasonably the most complex Python code ever: it counts 2'400'000 lines.