问题描述
感谢您提供帮助,我是学校主要工作的初级程序员,并决定做一个结帐应用程序。这不是什么太花哨的东西,大多数方面都是硬编码的,其中一些片段是从可编辑的 txt 文件中读取的。我目前正在制作应用程序的主系统,用户可以在其中选择添加到购物车的项目,并检查它是否已经在购物车中以修改购物车,因为我不想加倍在一件衣服上。
###Tasks 我被卡住了
- 我无法让它写入一个新行(大约 234 行),它应该接受 \n,它不起作用,而是显示一个框,好像它不理解字符。可能是我使用的字符系统,但我不太确定。 如果购物车中有东西并且添加了另一东西,则无法返回并修改购物车中已有的东西。 (我可以在之后没有添加任何其他内容时执行此操作,因为它只是替换了文本)。 我的老师/项目主管还说要添加一个管理功能,他们可以通过 txt 文件修改项目、价格和图片(如果我添加了)。
- 我还希望购物车位于滚动屏幕上,并在必要时更新大小,但这是一项可选功能,除非有人可以解释,否则我将寻找教程。 顶部的矩形是为了带到另一个页面,有更多的选项可供选择,我想不出如何编码这一点。 我也有点想在右下角打印日期和时间,但这是一个小部分 我知道很多,但我要到六月下旬才能完成所有事情。感谢任何建议,因为它仍然是一项正在进行的大型工作,我只是语言的新手。如果您需要任何其他相关信息,请告诉我。
# window ----------------------------------------
"""
This file is the executable file which calls all other files including the
full application.
"""
#The following imports are modules which have prewritten
#functions which I am able to use. They came predownloaded
#with python as well as downloading some from pygame.org
import pygame,sys
import tkinter as tk
from tkinter import Tk,Canvas,Frame,BOTH
from tkinter import ttk
from tkinter import *
from collections import Counter as count
from datetime import datetime
import time
import string
#import read
mainClock = pygame.time.Clock()
#setting window
from pygame.locals import *
#The following are Constants which are defined here for easier maintanence
click = bool
pygame.init()
pygame.display.set_caption('LeVOGUE')
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1200,800),32)
windowSurface = pygame.display.set_mode((1200,650),pygame.DOUBLEBUF)
run = 2
font = pygame.font.SysFont(None,35)
font2 = pygame.font.SysFont(None,70)
font3 = pygame.font.SysFont(None,25)
base_font = pygame.font.Font(None,50)
base_font2 = pygame.font.Font(None,160)
Item = ''
Item_rect = pygame.Rect(780,33,400,547)
Colour = pygame.Color(0,0)
found =False
#key_text_num = 2
s = " "
fh =open("user_info.txt","r")
keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN
Volume = True
active = False
#tick_active = False
#This is responsible fo importing the mixer module
from pygame import mixer
#Load music file
mixer.music.load("MainMenuMusic.mp3")
#play music file
mixer.music.play()
sc1_num = 0
sc1_price = 0
sc2_num = 0
sc2_price = 0
def Main_sales():
#this area of the code was reused from a prevIoUs file
#which is an example of the RAD approach.
#~~~~~~~~~~~~~~~~~Start Code applied from prevIoUs programs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global click
global Item
global active
global bags
global current_time
global found
global count
global s
global fh
global i_a
global key_text_num
global Item_rect
global sc1_num
global sc1_price
global sc2_num
global sc2_price
#This checkBox is imported from a module found within the LeVOGUE folder
#The checkBox file is imported above and called with the function checkBox
while True:
#Defining Quit
for event in pygame.event.get():
if event.type == QUIT:
print("Quit")
pygame.quit()
sys.exit()
#~~~~~~~~~~~~~~~~~~~~~~~~End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if key escape key is pressed,Quit
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#if click,click is true
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
if click:
print(pygame.mouse.get_pos())
#Background colour
screen.fill((255,255,255))
text_surface = font3.render(Item,True,(0,0))
screen.blit(text_surface,(Item_rect.x+20,Item_rect.y +100))
#mouse position
mx,my = pygame.mouse.get_pos()
#This section of code was applied from a tutorial which I followed
#It was used to allow me to create rectangular buttons although it was
#heavily modified to add functionality to different buttons
#~~~~~~~~~~~~~~Start code applied from tutorial ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b_w = 180
b_h = 70
b2_x = 274
b2_y = 562
b2_w = 140
b2_h = 65
b3_x = 55
b3_y = 200
b4_x = 275
b4_y = 200
b5_x = 490
b5_y = 200
b6_x = 55
b6_y = 290
b7_x = 275
b7_y = 290
b8_x = 490
b8_y = 290
b9_x = 55
b9_y = 380
b10_x = 275
b10_y = 380
b11_x = 490
b11_y = 380
b12_x = 55
b12_y = 470
b13_x = 275
b13_y = 470
b14_x = 490
b14_y = 470
button_2 = pygame.Rect(b2_x,b2_y,b2_w,b2_h)
button_3 = pygame.Rect( b3_x,b3_y,b_w,b_h)
button_4 = pygame.Rect( b4_x,b4_y,b_h)
button_5 = pygame.Rect( b5_x,b5_y,b_h)
button_6 = pygame.Rect( b6_x,b6_y,b_h)
button_7 = pygame.Rect( b7_x,b7_y,b_h)
button_8 = pygame.Rect( b8_x,b8_y,b_h)
button_9 = pygame.Rect( b9_x,b9_y,b_h)
button_10 = pygame.Rect( b10_x,b10_y,b_h)
button_11 = pygame.Rect( b11_x,b11_y,b_h)
button_12 = pygame.Rect( b12_x,b12_y,b_h)
button_13 = pygame.Rect( b13_x,b13_y,b_h)
button_14 = pygame.Rect( b14_x,b14_y,b_h)
#What happens if the mouse click is over buttons listed above
if button_2.collidepoint(mx,my):
if click:
print("button2")
import MainMenu
if button_3.collidepoint(mx,my):
if click:
#while (sc_num < 10):
if (f"Scrunchie {sc1_num} ${sc1_price} ") in Item:
Item = Item[:-29]
sc1_num = sc1_num+1
sc1_price = sc1_price+15
Item = Item + (f"Scrunchie {sc1_num} ${sc1_price} \n")
print(" Button3")
if button_4.collidepoint(mx,my):
if click:
if (f"Scrunchie {sc2_num} ${sc2_price}") in Item:
Item = Item[:-39]
sc2_num = sc2_num+1
sc2_price = sc2_price+15
Item = Item + (f"Scrunchie2 {sc2_num} ${sc2_price}")
print(" Button4")
if button_5.collidepoint(mx,my):
if click:
print(" Button5")
if button_6.collidepoint(mx,my):
if click:
print(" Button6")
if button_7.collidepoint(mx,my):
if click:
print(" Button7")
if button_8.collidepoint(mx,my):
if click:
print(" Button8")
if button_9.collidepoint(mx,my):
if click:
print(" Button9")
if button_10.collidepoint(mx,my):
if click:
print(" Button10")
if button_11.collidepoint(mx,my):
if click:
print(" Button11")
if button_12.collidepoint(mx,my):
if click:
print(" Button12")
if button_13.collidepoint(mx,my):
if click:
print(" Button13")
if button_14.collidepoint(mx,my):
if click:
print(" Button14")
text_surface_Cart = font.render("Cart",0))
text_surface_Item = font.render( "Item Qty Price",0))
screen.blit(text_surface_Cart,(790,50))
screen.blit(text_surface_Item,100))
#for Button 2
text_surface_Enter = font2.render("Enter",0))
screen.blit(text_surface_Enter,pygame.Rect((280,565),(120,70)))
#Cart
pygame.draw.rect(screen,Colour,pygame.Rect(780,30,550),2)
#button 2; Enter
pygame.draw.rect(screen,button_2,2)
#Draw Boxes for the number buttons
pygame.draw.rect(screen,button_3,2)
pygame.draw.rect(screen,button_4,button_5,button_6,button_7,button_8,button_9,button_10,button_11,button_12,button_13,button_14,2)
screen.blit((font3.render("Scrunchie1",0))),(b3_x+20,b3_y+15))
screen.blit((font3.render("Scrunchie2",(b4_x+20,b4_y+15))
screen.blit((font3.render("Scrunchie3",(b5_x+20,b5_y+15))
screen.blit((font3.render("Scrunchie4",(b6_x+20,b6_y+15))
screen.blit((font3.render("Scrunchie5",(b7_x+20,b7_y+15))
screen.blit((font3.render("Shirt6",(b8_x+20,b8_y+15))
screen.blit((font3.render("Shirt7",(b9_x+20,b9_y+15))
screen.blit((font3.render("Shirt8",(b10_x+20,b10_y+15))
screen.blit((font3.render("Shirt9",(b11_x+20,b11_y+15))
screen.blit((font3.render("Pants10",(b12_x+20,b12_y+15))
screen.blit((font3.render("Pants11",(b13_x+20,b13_y+15))
screen.blit((font3.render("Pants11",(b14_x+20,b14_y+15))
#~~~~~~~~~~~~~~~~~~ End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ButtonBolt = pygame.image.load("LionsHEAD.png")
ButtonBolt = pygame.transform.scale(ButtonBolt,120))
screen.blit(ButtonBolt,(20,15))
text_surface_LeVOGUE = font2.render("LeVOGUE",0))
screen.blit(text_surface_LeVOGUE,(140,55))
Key_Rect = pygame.Rect(260,125,230,40)
pygame.draw.rect(screen,0),Key_Rect,2)
Left_select_Rect = pygame.Rect(208,40,Left_select_Rect,2)
text_surface_Left_select = font2.render("<",0))
screen.blit(text_surface_Left_select,(215,118))
Right_select_Rect = pygame.Rect(502,Right_select_Rect,2)
text_surface_Left_select = font2.render(">",(510,118))
#CART
pygame.draw.rect(screen,2)
click = False
#update screen
pygame.display.update()
pygame.display.flip()
mainClock.tick(60)
#The following code was derived from a tutorial which allows the application
#to quit without causing any error codes.
#~~~~~~~~~~~~Start code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def Quit():
running = True
while running:
pygame.quit()
quit()
#~~~~~~~~~~~ End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#run the main menu
Main_sales()
pygame.quit()
sys.exit()
解决方法
在你的队伍中
text_surface = font3.render(Item,True,(0,0))
您正在使用 pygame 字体渲染器。 pygame.font.Font/SysFont().render() 不支持多行文本。
文档 here 中对此进行了说明。
文本只能是一行:不呈现换行符。