import random
import re
from tkinter import *
from XsApi import XsApi

class XinSheng(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.place()

        self.v = StringVar()
        self.v.set('全部')  # 设置默认选择值
        self.r1 = Radiobutton(root, text='全部', value='全部', variable=self.v, width=5)
        self.r1.place(x=40, y=10)
        self.r2 = Radiobutton(root, text='读书', value='750183756628320256', variable=self.v, width=5)
        self.r2.place(x=110, y=10)
        self.r3 = Radiobutton(root, text='活动', value='750183808587358208', variable=self.v, width=5)
        self.r3.place(x=180, y=10)
        self.r4 = Radiobutton(root, text='旅行', value='750183842783518720', variable=self.v, width=5)
        self.r4.place(x=250, y=10)
        self.r5 = Radiobutton(root, text='摄影', value='750183867982897152', variable=self.v, width=5)
        self.r5.place(x=320, y=10)
        self.r6 = Radiobutton(root, text='运动', value='750183971586400256', variable=self.v, width=5)
        self.r6.place(x=390, y=10)
        self.r7 = Radiobutton(root, text='健康', value='750184000946528256', variable=self.v, width=5)
        self.r7.place(x=460, y=10)
        self.r8 = Radiobutton(root, text='其他', value='750184094362066944', variable=self.v, width=5)
        self.r8.place(x=530, y=10)
        # part查询按钮
        self.button_001 = Button(root, text="随机选择", command=lambda: self.get_list(), font='微软雅黑 9', width=7,
                                 fg='blue', height=1)
        self.button_001.place(x=600, y=10)

        self.copy_button = Button(root, text="拷贝日志", command=lambda: self.copy(), font='微软雅黑 9', width=7
                                  , height=1)
        self.copy_button.place(x=670, y=10)

        self.clear_button = Button(root, text="清除日志", command=lambda: self.clear(), font='微软雅黑 9', fg='red',
                                   width=7, height=1)
        self.clear_button.place(x=740, y=10)

        self.area_text_001 = Text(root, width=80, height=1, bg='#ccccb3', font='Arial 12')
        self.area_text_001.place(x=40, y=45)
        self.area_text_001.insert(INSERT, '标题')
        # 创建多行文本框
        self.area_text_002 = Text(root, width=80, height=28, bg='#ccccb3', font='Arial 12', wrap='word')
        self.area_text_002.place(x=40, y=70)
        self.area_text_002.insert(INSERT, '内容')

    def get_list(self):
        category_id = self.v.get()
        if category_id == '全部':
            category_id = ''
        print(f"category_id: {category_id}")
        list_resp = XsApi().get_list(category_id=category_id)
        index = random.randint(0, 50)
        try:
            title = list_resp["data"]["list"][index]["title"]
            uuid = list_resp["data"]["list"][index]["uuid"]
        except:
            title = "unknow error"
            uuid = "unknow error"
        info_resp = XsApi().get_info(uuid=uuid)
        try:
            data = info_resp["data"]["fields"][1]["fieldValue"]
            data = re.findall('>(.*?)<', data)
            content = ' '.join(data)
        except:
            content = "出错了"
        self.area_text_001.delete(1.0, END)
        self.area_text_001.insert(INSERT, chars=title)
        self.area_text_002.delete(1.0, END)
        self.area_text_002.insert(INSERT, chars=content)

    def copy(self):
        content = self.area_text_002.get(1.0, END)
        self.clipboard_clear()
        self.clipboard_append(content)

    def clear(self):
        self.area_text_001.delete(1.0, END)
        self.area_text_002.delete(1.0, END)


if __name__ == '__main__':
    root = Tk()
    root.title('心声社区')
    # 获取屏幕宽度和高度
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    start_x = 800
    start_y = 600
    # 计算窗口位置
    x = (screen_width - start_x) // 2
    y = (screen_height - start_y) // 2
    root.geometry(f'{start_x}x{start_y}+{x}+{y}')
    # 设置背景颜色
    root.configure(bg='lightblue')
    t = XinSheng(master=root)
    root.mainloop()
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐