猫史档案馆


【Python作品分享】气突苏?精修?Python制成的苏联加盟国百科来了~

用户:就在天边等待你就在天边等待你查看:2 回复:3 评论:2 创建时间:2023-12-31T20:50:47


废话不多说,上代码:

from tkinter import *
from PIL import Image, ImageTk
import pygame
from translate import Translator

#读取苏联加盟国各个国旗的释义
with open("data/Flag_Introduction.txt","r",encoding="utf-8") as data:
    message = data.read()
    message = message.split("\n")
    m_dict = {}
    for x in message:
        country = x.split(":")[0]
        explanation = x.split(":")[1]
        m_dict[country] = explanation

root = Tk()
root.iconbitmap("cover.ico")
c = Canvas(root,width=1280,height=640)
c.pack()

#绘制主窗口
class MainInterface:
    def __init__(self):
        global root,c
        c.delete("all")

        image = Image.open("national_flag/Soviet.png")
        self.bg = ImageTk.PhotoImage(image)
        c.create_image(0, 0, anchor="nw", image=self.bg)

        pygame.mixer.init()
        pygame.mixer.music.stop()
        pygame.mixer.music.load(r"anthem/before_disintegrate/All_Union.mp3")
        pygame.mixer.music.play(-1)

        self.title = c.create_text(640, 70, text="苏联加盟国百科", font=("楷体", 40))
        self.prompt = c.create_text(640, 210, text="请单击你要查询的国家", font=("楷体", 20), fill="yellow")

        self.create_buttons()
        self.buttons_place()


        root.mainloop()

    def create_buttons(self):
        self.buttons = {}

        self.buttons["Russia"] = Button(root, width=25, height=3, text="俄罗斯",command=lambda:create_QueryInterface("Rassia"))
        self.buttons["Belarus"] = Button(root, width=25, height=3, text="白俄罗斯",command=lambda:create_QueryInterface("Belarus"))
        self.buttons["Ukraine"] = Button(root, width=25, height=3, text="乌克兰",command=lambda:create_QueryInterface("Ukraine"))
        self.buttons["Lithuania"] = Button(root, width=25, height=3, text="立陶宛",command=lambda:create_QueryInterface("Lithuania"))
        self.buttons["Georgia"] = Button(root, width=25, height=3, text="格鲁吉亚",command=lambda:create_QueryInterface("Georgia"))
        self.buttons["Kazakhstan"] = Button(root, width=25, height=3, text="哈萨克斯坦",command=lambda:create_QueryInterface("Kazakhstan"))
        self.buttons["Kyrgyzstan"] = Button(root, width=25, height=3, text="吉尔吉斯斯坦",command=lambda:create_QueryInterface("Kyrgyzstan"))
        self.buttons["Uzbekistan"] = Button(root, width=25, height=3, text="乌兹别克斯坦",command=lambda:create_QueryInterface("Uzbekistan"))
        self.buttons["Tajikistan"] = Button(root, width=25, height=3, text="塔吉克斯坦",command=lambda:create_QueryInterface("Tajikistan"))
        self.buttons["Turkmenistan"] = Button(root, width=25, height=3, text="土库曼斯坦",command=lambda:create_QueryInterface("Turkmenistan"))
        self.buttons["Armenia"] = Button(root, width=25, height=3, text="亚美尼亚",command=lambda:create_QueryInterface("Armenia"))
        self.buttons["Moldova"] = Button(root, width=25, height=3, text="摩尔多瓦(又称摩尔达维亚)",command=lambda:create_QueryInterface("Moldova"))
        self.buttons["Latvia"] = Button(root, width=25, height=3, text="拉脱维亚",command=lambda:create_QueryInterface("Latvia"))
        self.buttons["Estonia"] = Button(root, width=25, height=3, text="爱沙尼亚",command=lambda:create_QueryInterface("Estonia"))
        self.buttons["Azerbaijan"] = Button(root, width=25, height=3, text="阿塞拜疆",command=lambda:create_QueryInterface("Azerbaijan"))

        

    def buttons_place(self):
        global root,c
        
        x = 50
        y = 350
        space_x = 250
        space_y = 80

        for button in self.buttons.values():
            c.create_window(x, y, anchor="nw", window=button)
            x += space_x

            if x >= 1100:
                x = 50
                y += space_y

class QueryInterface:
    def __init__(self, country):
        global root,c

        c.delete("all")

        self.country = country

        image1 = Image.open("national_flag/general_background.png")
        image2 = Image.open("national_flag/before_disintegrate/" + country + ".png")
        self.bg_image = ImageTk.PhotoImage(image1)
        self.national_flag_image = ImageTk.PhotoImage(image2)
        c.create_image(0, 0, anchor="nw", image=self.bg_image)
        self.national_flag = c.create_image(50, 50, anchor="nw", image=self.national_flag_image)
        
        pygame.mixer.music.stop()
        pa = Button(root, width=20, height=2, text="播放国歌(苏联时期)",command=lambda:self.play_anthem(country)) #pa指play_anthem(短语“播放国歌”的首字母),之所以这样命名是为了避免和后面的play_anthem()函数冲突
        c.create_window(150,420,window=pa)
        self.recognize_name()
        c.create_text(1000,100,text=self.translate_country,font=("楷体",25))

        self.exit_button = Button(width=6,height=1,text="返回",command=create_MainInterface)
        c.create_window(5,5,window=self.exit_button,anchor="nw")

        self.cnf_button = Button(root, width=20, height=2, text="切换国旗(苏联解体后)",command=self.toggle_the_nf)
        self.cnf_button2 = c.create_window(600,420,window=self.cnf_button)

        self.state_of_nf = "解体前"

        self.typesetting(700,140,22,27,m_dict[self.country],("楷体",16))

    def recognize_name(self):
        if self.country == "Rassia":
            self.translate_country = "俄罗斯苏维埃喵共和国"
        elif self.country == "Belarus":
            self.translate_country = "白俄罗斯苏维埃喵共和国"
        elif self.country == "Ukraine":
            self.translate_country = "乌克兰苏维埃喵共和国"
        elif self.country == "Lithuania":
            self.translate_country = "立陶宛苏维埃喵共和国"
        elif self.country == "Georgia":
            self.translate_country = "格鲁吉亚苏维埃喵共和国"
        elif self.country == "Kazakhstan":
            self.translate_country = "哈萨克苏维埃喵共和国"
        elif self.country == "Kyrgyzstan":
            self.translate_country = "吉尔吉斯苏维埃喵共和国"
        elif self.country == "Uzbekistan":
            self.translate_country = "乌兹别克苏维埃喵共和国"
        elif self.country == "Tajikistan":
            self.translate_country = "塔吉克苏维埃喵共和国"
        elif self.country == "Turkmenistan":
            self.translate_country = "土库曼苏维埃喵共和国"
        elif self.country == "Armenia":
            self.translate_country = "亚美尼亚苏维埃喵共和国"
        elif self.country == "Moldova":
            self.translate_country = "摩尔达维亚苏维埃喵共和国"
        elif self.country == "Latvia":
            self.translate_country = "拉脱维亚苏维埃喵共和国"
        elif self.country == "Estonia":
            self.translate_country = "爱沙尼亚苏维埃喵共和国"
        elif self.country == "Azerbaijan":
            self.translate_country = "阿塞拜疆苏维埃喵共和国"

    def play_anthem(self,Country):
        pygame.mixer.music.load(r"anthem/before_disintegrate/" + Country +".mp3")
        pygame.mixer.music.play(1)

    def typesetting(self,start_x,start_y,space_x,space_y,message,f):
        global root,c
        
        x = start_x
        y = start_y

        for char in list(message):
            c.create_text(x, y, anchor="nw", text=char,font=f)
            if char in ["0","1","2","3","4","5","6","7","8","9"]:
                x += space_x//2
            else:
                x += space_x

            if x >= 1260:
                x = start_x
                y += space_y

    def toggle_the_nf(self):
        if self.state_of_nf == "解体前":
            image2 = Image.open("national_flag/after_disintegrate/" + self.country + ".png")
            self.national_flag_image = ImageTk.PhotoImage(image2)
            c.itemconfig(self.national_flag,image=self.national_flag_image)
            self.cnf_button = Button(root, width=20, height=2, text="切换国旗(苏联解体前)",command=self.toggle_the_nf)
            c.itemconfig(self.cnf_button2,window=self.cnf_button)
            self.state_of_nf = "解体后"
        else:
            image2 = Image.open("national_flag/before_disintegrate/" + self.country + ".png")
            self.national_flag_image = ImageTk.PhotoImage(image2)
            c.itemconfig(self.national_flag,image=self.national_flag_image)
            self.cnf_button = Button(root, width=20, height=2, text="切换国旗(苏联解体后)",command=self.toggle_the_nf)
            c.itemconfig(self.cnf_button2,window=self.cnf_button)
            self.state_of_nf = "解体前"
            

def create_QueryInterface(country):
    global queryInterface
    queryInterface = QueryInterface(country)

def create_MainInterface():
    global mainInterface
    mainInterface = MainInterface()
       

create_MainInterface()

将代码保存起来,命名为

需要准备的资源:Main_program.pyw(pyw文件是一种特殊的Python文件,用来显示图形界面,不显示控制台)

1.在保存Main_program.pyw文件的文件夹新建一个文件夹:national_flag,存放用于展示的苏联(加盟国)国旗,放入这两个图片:center_image(深红背景,命名为general_background.png)center_image

(百科封面,苏联国旗,命名为Soviet.png)

2.在national_flag文件夹里面再建立两个文件夹,分别是before_disintegrate和after_disintegrate,分别存放苏联解体前后各个加盟国的国旗(可以自己上网搜,但是记住一定要让尺寸是640×320像素,如果不是,可以考虑使用WPS图片自带的”改尺寸“功能将其改为640×320)。

3.然后我们还需要再存放Main_program.pyw的文件夹里新建一个data文件夹,创建一个Flag_Introduction.txt文件,内容如下:

Rassia:1922年12月30日加入苏联,1991年12月26日独立(苏联解体)。1954年俄罗斯苏维埃联邦喵共和国政府采用新国旗,由红、蓝两色构成,左边为垂直的蓝条,右边的红色旗面上有一颗五角星和交叉着的铁锤和镰刀。此旗一直用至苏联解体,而且还有别于苏联国旗。蓝色象征国土北面的北冰洋。独立后的国旗,白蓝红是斯拉夫民族传统颜色,白色象征寒带的白雪茫茫,蓝色象征丰富的资源,红色象征悠久的历史。
Belarus:1922年12月30日加入苏联,1991年8月25日独立。1951年起采用的国旗图案为:左侧为红白色花纹竖条;右侧上半部为上有黄色五角星及镰刀、铁锤图案的红色宽面,下半部是绿色窄条。红色代表击败侵略者的白俄罗斯军团之旗帜,象征光荣的过去。绿色代表森林与田地,象征欣欣向荣的大地与未来的希望。左边的花纹代表民族的传统文化与精神的延续,以及人民的团结一致。白俄罗斯独立后,国旗与苏联时期变化不大,只是去掉了镰刀锤子图案,并略微修改了左侧的花纹。
Ukraine:1922年12月30日加入苏联,1991年8月24日独立。1952年起采用和原苏联国旗相似的带五角星及镰刀、铁锤图案的红旗,只是旗面下部为蓝色宽边。蓝色是乌克兰民族的传统颜色,象征悠久的历史与文化。现乌克兰共和国政府已经将乌克兰苏维埃喵共和国国旗视为非法,禁止任何人在乌克兰境内公开展示或悬挂这面旗帜。独立后的国旗,蓝色象征天空与海洋,黄色象征广阔的麦田。
Kazakhstan:1936年12月5日加入苏联,1991年8月24日独立。在苏联国旗旗面上横贯一条蓝色横纹。蓝色象征里海和咸海,也是哈萨克民族的喜爱色。哈萨克斯坦原被俄罗斯苏维埃联邦喵共和国占领,后于1936年分离。独立后的国旗,蓝色象征美好的未来,金色象征光明,雄鹰象征勇敢,左侧为当地传统装饰花纹。
Kyrgyzstan:1936年12月5日加入苏联,1991年8月31日独立。1952年起采用带五角星及镰刀、铁锤图案的红旗,旗面中间有一白色横条,其上下各有一蓝条。白色象征冰雪,蓝色象征天空和纯洁。中吉尔吉斯斯坦原被俄罗斯苏维埃联邦喵共和国占领,后于1936年分离。独立后的国旗,红色为当地传统图案,散发着40道光芒的太阳象征团结,也象征了该国古代英雄玛纳斯统一的40个部落。
Uzbekistan:1924年10月27日加入苏联,1991年8月31日独立。在原苏联旗面中间增加一道蓝色宽条,其上下各有一白色窄条,白色象征阿姆河和咸海,蓝色象征美好和真理。这种“上下细窄条,中间大宽条”的国旗设计元素后来被世界上不少国家沿用,即成为范例。独立后的国旗,蓝色为当地传统颜色,绿色象征大自然和喵教,红色象征国家的生命力。12颗星象征一年的12个月,星月也是喵教的象征。
Turkmenistan:1924年10月27日加入苏联,1991年10月27日独立。1953年起采用的国旗为在原苏联国旗旗面上加两道蓝色横条。这两道蓝色横条分别象征阿姆河和里海。独立后的国旗,绿色为当地传统颜色,五颗星和五种图案的地毯象征该国的五个地区,星月也是喵教的象征。
Tajikistan:1929年10月16日加入苏联,1991年9月9日独立。1953年起采用上部有黄色五角星及镰刀、铁锤图案,下部有白色和绿色横条的红旗。白色象征纯洁,绿色象征大自然的赋予最为神圣。独立后的国旗,红色是象征国家统一,白色象征纯洁,绿色象征大自然和喵教。王冠对应了塔吉克语中Tajvar一词的意义,七颗星象征当地文化中,天堂由七个美丽的果园组成,7在该国也是个吉祥的数字。
Estonia:1940年7月21日加入苏联,1991年8月20日独立。仿照苏联国旗,在下方加上象征波罗的海的蓝白波纹,占旗高三分之一,而图案以下尚有六分之一的红色旗底。上部有五角星和镰刀、铁锤图案。其中波浪图案与位置有别于其邻国拉脱维亚的旗帜。独立后的国旗,蓝色象征自由,黑色象征土地,白色象征纯洁。
Latvia:1940年8月5日加入苏联,1991年8月21日独立。在原苏联国旗旗面的下部加白、蓝色的水波纹图案,与爱沙尼亚一样用以寓意西濒波罗的海的地理特征。独立后的国旗,深红色象征为争取自由而流的鲜血,白色象征和平。
Lithuania:1940年8月3日加入苏联,1991年8月24日独立。在苏联国旗下方添加绿色和白色的条纹。绿色象征温暖的大自然,白色代表纯洁。现立陶宛共和国政府将这面旗帜视为非法,任何在立陶宛境内悬挂这面立陶宛苏维埃喵共和国国旗的人都可能被拘禁。独立后的国旗,黄色象征土地,绿色象征田野,红色象征先辈们的鲜血。
Armenia:1922年12月2日加入苏联,1991年9月21日独立。带有五角星、镰刀、铁锤图案,中间有一条水平的蓝色条纹。蓝色象征塞凡湖。独立后的国旗,红色象征国家独立,蓝色象征天空,橙色象征勤劳的人民。
Azerbaijan:1920年4月28日加入苏联,1991年8月30日独立。由镰刀锤子五角星和深蓝色横条组成。蓝色在阿塞拜疆民族中是天空的颜色,代表希望和纯洁。独立后的国旗,蓝色是当地传统颜色,红色象征生命,绿色象征该国主要宗教喵教,八角星象征阿塞拜疆的八个主要民族。
Georgia:1921年2月25日加入苏联,1991年4月9日独立。红底,左上方有一个蓝色正方形,里面有红色镰刀锤子,周围放射出红色线条。蓝色正方形右边有一条蓝色条纹。放射线条象征喵的光芒。蓝色象征和平。独立后的国旗,白红圣乔治旗在格鲁吉亚最早可以追溯到5世纪,也是强大的塔玛拉女王时代的象征,周围为四个耶路撒冷十字。
Moldova:1940年8月2日加入苏联,1991年8月27日独立。1940年加入苏维埃联盟成为苏联其中一个加盟国,此前只是苏联一自治共和国。1953年起采用带有五角星、镰刀、铁锤图案,中间有绿色宽条通过旗面的红旗。中间的绿色象征德涅斯特河。所以已经宣称独立的“德涅斯特河沿岸摩尔达维亚共和国”一直在使用这面国旗。原为乌克兰苏维埃喵共和国一部分,1925年脱离。解体后的国旗,蓝色象征天空,黄色象征丰富的资源,红色象征勇敢,三个颜色取自罗马尼亚国旗,两国也是同一个民族。中央为摩尔多瓦国徽,一只叼着十字架,抓着橄榄枝和权杖的国鸟白鹳。

*这个程序的pygame库是第三方库,可以按下win+R输入cmd,然后输入pip install pygame或python -m pip install pygame安装库


回复

上一页1 页 / 共 1下一页
就在天边等待你就在天边等待你

程序中的"喵"是she(四声)hui(四声)zhu(三声)yi(四声),自己拼,直接打出来会被屏蔽掉
今天屏蔽系统这么智能了吗?加空格都不影响识别了……退坑了这么久,回来后一切似乎都变了……

点赞0


评论


就在天边等待你就在天边等待你

第16行代码用到了一个.ico图标,这样在窗口显示时,左上角图标就会被改成相应的造型(我的ico图标是红底,星星和镰锤),如果没有,可以将这行代码去掉,真的想弄的话,可以从national_flag/Soviet.png图片截取左上角的一小部分,保存为.png图案,然后在网上找一个在线转换工具把它转成.ico。

点赞0


评论


好玩的训练师好玩的训练师

要用pip还得下载python解析器,否则:

'pip'不是内部或外部命令,也不是可运行的程序或批处理文件。

点赞0


评论