TK画布中打印程序运行
时间: 2024-05-04 17:17:09 浏览: 72
在 TK 画布中打印程序运行,可以使用 `print()` 函数将输出打印到控制台或者文本框中。以下是一个简单的示例:
```python
import tkinter as tk
root = tk.Tk()
# 创建一个文本框
text_box = tk.Text(root)
text_box.pack()
# 定义一个函数,将输出打印到文本框中
def print_to_text_box(text):
text_box.insert(tk.END, text + '\n')
# 打印一些信息
print_to_text_box('Hello, World!')
print_to_text_box('This is a test.')
root.mainloop()
```
在上面的代码中,我们使用 `Text` 控件创建了一个文本框,并定义了一个 `print_to_text_box()` 函数,它将输出打印到文本框中。在程序中调用这个函数,就可以将输出打印到文本框中了。
相关问题
请给我个排列多张大小不一且不能做任何修改的矩形图片放在一张宽固定可无限延长的画布上且使这个画布面积最小的Python程序用可视化的窗口显示这过程不使用pygame
以下是一个基于 Python 的解法,使用 tkinter 库来实现可视化窗口显示。
思路:
1. 将所有矩形按面积从大到小排序
2. 遍历每个矩形,将其放入画布中最小的空白处
3. 重复步骤 2 直到所有矩形都被放置
代码如下:
```python
import tkinter as tk
# 矩形类
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
# 返回矩形面积
def area(self):
return self.width * self.height
# 获取矩形宽度和高度
def get_size(self):
return (self.width, self.height)
# 排列矩形
def pack_rectangles(rectangles):
# 将矩形按面积从大到小排序
rectangles.sort(key=lambda r: r.area(), reverse=True)
# 初始化画布
canvas_width = max(r.width for r in rectangles)
canvas_height = sum(r.height for r in rectangles)
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg="white")
canvas.pack()
# 遍历每个矩形
current_y = 0
for rectangle in rectangles:
# 获取矩形宽度和高度
width, height = rectangle.get_size()
# 查找最小空白处
x, y = find_empty_spot(canvas, width, height, current_y)
# 将矩形放入画布
canvas.create_rectangle(x, y, x+width, y+height, fill="gray")
current_y = y + height
# 返回画布面积
return canvas_width * canvas_height
# 查找最小空白处
def find_empty_spot(canvas, width, height, start_y):
x = 0
y = start_y
while True:
# 查找下一个空白处
overlaps = canvas.find_overlapping(x, y, x+width, y+height)
if not overlaps:
return (x, y)
# 如果当前位置已被占用,则移到下一行
x = canvas.bbox(overlaps[-1])[2]
if x + width > canvas.winfo_width():
x = 0
y = canvas.bbox(overlaps[0])[3]
# 创建矩形列表
rectangles = [Rectangle(60, 80), Rectangle(40, 100), Rectangle(80, 40), Rectangle(50, 50), Rectangle(100, 60)]
# 排列矩形
canvas_area = pack_rectangles(rectangles)
# 显示画布面积
print("Canvas area:", canvas_area)
# 运行可视化窗口
root = tk.Tk()
root.title("Packing Rectangles")
root.mainloop()
```
这段代码会创建一个可视化窗口,其中包含排列好的矩形。在运行时,程序会计算出画布的最小面积,并将其打印到控制台上。
用python编写一个字符界面的五子棋人机对战程序,游戏难度中等,代码简单
好的,我可以给你提供一个简单的五子棋人机对战程序。这个程序使用Python的Tkinter库来创建字符界面,并使用Minimax算法作为AI。这个程序的难度可以被调整为中等。
代码如下:
```python
import tkinter as tk
import random
# 创建棋盘
def create_board():
board = []
for i in range(15):
row = []
for j in range(15):
row.append(0)
board.append(row)
return board
# 判断胜利
def check_win(board, player):
for i in range(15):
for j in range(11):
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
return True
for i in range(11):
for j in range(15):
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
return True
for i in range(11):
for j in range(11):
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
return True
for i in range(11):
for j in range(4, 15):
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
return True
return False
# 计算得分
def calculate_score(board, player):
score = 0
for i in range(15):
for j in range(11):
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == 0:
score += 100
if board[i][j] == 0 and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
score += 100
if board[i][j] == player and board[i][j+1] == 0 and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
score += 100
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == 0 and board[i][j+3] == player and board[i][j+4] == player:
score += 100
if board[i][j] == player and board[i][j+1] == player and board[i][j+2] == player and board[i][j+3] == player and board[i][j+4] == player:
score += 1000
for i in range(11):
for j in range(15):
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == 0:
score += 100
if board[i][j] == 0 and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
score += 100
if board[i][j] == player and board[i+1][j] == 0 and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
score += 100
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == 0 and board[i+3][j] == player and board[i+4][j] == player:
score += 100
if board[i][j] == player and board[i+1][j] == player and board[i+2][j] == player and board[i+3][j] == player and board[i+4][j] == player:
score += 1000
for i in range(11):
for j in range(11):
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == 0:
score += 100
if board[i][j] == 0 and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
score += 100
if board[i][j] == player and board[i+1][j+1] == 0 and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
score += 100
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == 0 and board[i+3][j+3] == player and board[i+4][j+4] == player:
score += 100
if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player and board[i+4][j+4] == player:
score += 1000
for i in range(11):
for j in range(4, 15):
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == 0:
score += 100
if board[i][j] == 0 and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
score += 100
if board[i][j] == player and board[i+1][j-1] == 0 and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
score += 100
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == 0 and board[i+3][j-3] == player and board[i+4][j-4] == player:
score += 100
if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player and board[i+4][j-4] == player:
score += 1000
return score
# AI
def minimax(board, depth, player):
if depth == 0:
return None, calculate_score(board, 2)
if player == 2:
best_score = float('-inf')
best_move = None
for i in range(15):
for j in range(15):
if board[i][j] == 0:
board[i][j] = 2
score = minimax(board, depth-1, 1)[1]
board[i][j] = 0
if score > best_score:
best_score = score
best_move = (i, j)
return best_move, best_score
else:
best_score = float('inf')
best_move = None
for i in range(15):
for j in range(15):
if board[i][j] == 0:
board[i][j] = 1
score = minimax(board, depth-1, 2)[1]
board[i][j] = 0
if score < best_score:
best_score = score
best_move = (i, j)
return best_move, best_score
# 让AI下棋
def ai_turn(board):
move = minimax(board, 3, 2)[0]
board[move[0]][move[1]] = 2
return move[0], move[1]
# 棋盘点击事件
def board_click(event):
global board, player_turn, game_over
if game_over:
return
x = event.x // 30
y = event.y // 30
if board[y][x] != 0:
return
board[y][x] = 1
player_turn = False
if check_win(board, 1):
game_over = True
canvas.create_text(225, 225, text='You win!', font=('Helvetica', 32), fill='red')
return
if not any(0 in row for row in board):
game_over = True
canvas.create_text(225, 225, text='Tie game!', font=('Helvetica', 32), fill='red')
return
x, y = ai_turn(board)
canvas.create_oval(x*30+5, y*30+5, x*30+25, y*30+25, fill='black')
if check_win(board, 2):
game_over = True
canvas.create_text(225, 225, text='You lose!', font=('Helvetica', 32), fill='red')
return
if not any(0 in row for row in board):
game_over = True
canvas.create_text(225, 225, text='Tie game!', font=('Helvetica', 32), fill='red')
return
# 创建窗口
window = tk.Tk()
window.title('Five in a Row')
window.geometry('450x450')
# 创建棋盘
board = create_board()
# 创建画布
canvas = tk.Canvas(window, width=450, height=450, bg='white')
canvas.pack()
# 画棋盘
for i in range(15):
canvas.create_line(15+i*30, 15, 15+i*30, 435)
canvas.create_line(15, 15+i*30, 435, 15+i*30)
# 点击事件
canvas.bind('<Button-1>', board_click)
# 游戏变量
player_turn = bool(random.getrandbits(1))
game_over = False
# 让AI先下
if not player_turn:
x, y = ai_turn(board)
canvas.create_oval(x*30+5, y*30+5, x*30+25, y*30+25, fill='black')
# 运行程序
window.mainloop()
```
运行程序后将会弹出一个棋盘界面。玩家可以用鼠标点击棋盘上的空格来下棋,黑色表示AI下的棋。当一方获得五个棋子连成一条线时,游戏结束并打印胜利者。如果棋盘被下满,游戏将以平局结束。
阅读全文