利用 tkinter 实现题库查询功能 [控件大小自适应界面变化]

今天回答了几个和 tkinter 相关的问题,都是和界面,控件,事件相关,结合这段时间都在手机答题, 题库查询太麻烦。就做了这个例子。

本例子用到 Tkinter 库。 包含Tkinter 的 Label , Entry , Button , Text 还有相关事件。 完成题库的查询显示功能。

控件大小自适应界面变化。

数据包含100道题目。

 

# encoding: utf-8
"""
@author: seakingx
@contact: hndm@qq.com
@version: 1.0
@file: doex.py
@time: 2020/3/19 0019 14:39

说明 简单查题工具
"""

import tkinter as tk
from tkinter import messagebox
on_hit = False
class MainBox:
    def __init__(self):
        self.AppName = "刷题【好好学习 天天向上】"
        self.size = '800x600'
        self.datalist = []
        self.init_data()

    def init_data(self):
        f = open('qgtk/qgtk.txt', encoding='utf8')
        txt_list = f.readlines()
        f.close()
        t_i = 0
        t_text = ""
        t_item = []
        t_list = []
        for t in txt_list:
            if t.strip() == "":
                continue
            if t.find("答案") >= 0:
                t_i = t_i + 1
                t_item.append(t_text.replace("\t", ""))
                t_item.append(t.replace("\t", ""))
                # t_list.append(t_item)
                self.datalist.append(t_item)
                t_text = ""
                t_item = []
            else:
                t_text = t_text + t
        # self.datalist = t_list[:]


    def window_begin(self):
        begin = tk.Tk()
        begin.title(self.AppName)
        begin.geometry(self.size)

        key_input = tk.Entry(begin, font=('微软雅黑', 16))
        key_input.place(relx=0.025, y=20, height=40, relwidth=0.50)



        def bksearch(ev = None):
            key_str = key_input.get()
            # print("查询{}".format(key_str))
            text_list.delete("1.0", tk.END)
            find_cnt = 0
            for t in self.datalist:
                if t[0].find(key_str)>0:
                    find_cnt = find_cnt + 1
                    text_list.insert("end",t[0])
                    text_list.insert("end", t[1])
            if find_cnt == 0:
                text_list.insert("end", "没有找到匹配的题目")
                info_str.set("没有找到匹配的题目")
            else:
                info_str.set("找到{}个题目".format(find_cnt))
            key_input.delete(0, "end")


        button = tk.Button(begin, text='查询', command=bksearch)
        button.place(relx=0.55, y=20, height=40, relwidth=0.10)

        info_str = tk.StringVar()
        info = tk.Label(begin, textvariable = info_str )
        info.place(relx=0.68, y=20, height=40, relwidth=0.30)


        key_input.bind("<Return>", bksearch)

        text_list = tk.Text(begin, font=('微软雅黑', 11))
        text_list.place(relx=0.025, rely=0.2, relheight=0.75, relwidth=.95)


        # 返回
        def callback():
            if messagebox.askokcancel("退出", "是否退出{}?".format(self.AppName)):
                begin.destroy()
        begin.protocol("WM_DELETE_WINDOW", callback)
        begin.mainloop()




if __name__=="__main__":
    window = MainBox()
    window.window_begin()

 

运行界面:

 

 

程序(包含100道题目)打包地址:

https://download.csdn.net/download/seakingx/12256558

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Python的Tkinter中,可以使用以下方法让件自适应屏幕大小: 1. 使用`place`方法:可以使用相对或绝对的坐标来定位件,让件随窗口大小改变而自适应。 ```python import tkinter as tk class App: def __init__(self, master): self.master = master self.label = tk.Label(master, text="Hello World") self.label.place(relx=0.5, rely=0.5, anchor='center') # 使用相对坐标,将标签置于窗口中心 root = tk.Tk() app = App(root) root.geometry("400x300") # 设置窗口大小 root.mainloop() ``` 2. 使用`grid`方法:可以使用行和列来定位件,让件随窗口大小改变而自适应。 ```python import tkinter as tk class App: def __init__(self, master): self.master = master self.label = tk.Label(master, text="Hello World") self.label.grid(row=0, column=0) # 将标签置于第一行第一列 root = tk.Tk() app = App(root) root.columnconfigure(0, weight=1) # 列0随窗口大小改变而自适应 root.rowconfigure(0, weight=1) # 行0随窗口大小改变而自适应 root.mainloop() ``` 3. 使用`pack`方法:可以将件放置在窗口的顶部、底部或中间等位置,让件随窗口大小改变而自适应。 ```python import tkinter as tk class App: def __init__(self, master): self.master = master self.label = tk.Label(master, text="Hello World") self.label.pack(fill=tk.BOTH, expand=True) # 将标签填充整个窗口并自适应大小 root = tk.Tk() app = App(root) root.mainloop() ``` 使用这些方法可以让件自适应屏幕大小,从而达到更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈年椰子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值