首页 > 其他分享 >tk的一个布局demo

tk的一个布局demo

时间:2023-08-19 17:55:51浏览次数:30  
标签:photo no demo 布局 current result text tk

效果

代码




import tkinter as tk
from tkinter import scrolledtext
import requests

def Q_A(data):
    headers = {
            'origin': 'https://chat2.jinshutuan.com',
            'referer': 'https://chat2.jinshutuan.com/',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        }
    session = requests.Session()
    session.trust_env = False
    json_data = {
        'prompt': data,
    }

    response = session.post('https://api.binjie.fun/api/generateStream', headers=headers, json=json_data)

    response_text = response.content.decode('utf-8')
    print(f'A:{response_text}')
    return response_text

def remove_placeholder(event):
    if result_entry.get("1.0", tk.END) == "这里输入你想问的问题\n":
        result_entry.delete("1.0", tk.END)
        result_entry.config(fg="black")  # Change text color to black

def display_result():
    result = result_entry.get("1.0", tk.END).strip()  # Get the text from the result entry
    if result:
        scrolled_text.delete(1.0, tk.END)  # Clear the current content

        scrolled_text.insert(tk.END, "请稍等,正在查询...")  # Display "请稍等,正在查询" first
        scrolled_text.update()  # Update the text widget to show the intermediate message

        Q = Q_A(result)  # Get the actual result

        scrolled_text.delete(1.0, tk.END)  # Clear the intermediate message
        scrolled_text.insert(tk.END, Q)  # Display the actual result

# 创建主窗口
root = tk.Tk()
root.iconbitmap(r'E:\Code\Python\Tkinter-py\Tk练习\天道酬勤.ico')
root.title("gpt问答")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(
    f'{int(screen_width * 0.75)}x{int(screen_height * 0.83)}+{int((screen_width - int(screen_width * 0.75)) / 2)}+{int((screen_height - int(screen_height * 0.83) - 40) / 2)}')

# root.resizable(0, 0)


# 创建一个输入框,用于输入结果
result_entry = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=40, height=10, font=('微软宋体', 15, 'bold'))
result_entry.insert(tk.END, '这里输入你想问的问题')
result_entry.config(fg="gray")  # Set text color to gray
result_entry.bind("<FocusIn>", remove_placeholder)  # Remove placeholder when focused
result_entry.pack(fill=tk.BOTH, pady=10, padx=5)

# 创建一个按钮,用于显示结果
frame1 = tk.Frame(root)
frame1.pack(fill=tk.BOTH, padx=5)
display_button = tk.Button(frame1, text="显示结果", command=display_result, bg='red', fg='white', relief=tk.RAISED)
# display_button.pack()
display_button.grid(row=0, column=0)

# 创建一个可滚动的文本框,用于显示结果
frame2 = tk.Frame(root)
frame2.pack(fill=tk.BOTH, expand=True)
scrolled_text = scrolledtext.ScrolledText(frame2, wrap=tk.WORD, width=40, height=10, font=('宋体', 15))
scrolled_text.pack(side=tk.LEFT,anchor='nw',fill=tk.BOTH ,pady=10, padx=5)  # Use expand=True to fill available space

# 图片查看器
import glob
from PIL import Image, ImageTk
photos = glob.glob('photo/*.jpg')
photos = [ImageTk.PhotoImage(Image.open(photo)) for photo in photos]

current_photo_no = 0
photo_label = tk.Label(frame2, image=photos[current_photo_no], width=300, height=400)
photo_label.pack()

number_var = tk.StringVar()
number_var.set('1 of 4')
tk.Label(frame2, textvariable=number_var, font=('微软雅黑', 12, 'bold'), relief=tk.SUNKEN, anchor=tk.CENTER).pack(fill=tk.X)

button_frame = tk.Frame(frame2)
button_frame.pack()

prev_photo = tk.Button(button_frame, text='上一页')
next_photo = tk.Button(button_frame, text='下一页')

prev_photo.pack(side=tk.LEFT, anchor=tk.CENTER)
next_photo.pack(side=tk.RIGHT, anchor=tk.CENTER)

def change_photos(next_no):
    global current_photo_no
    current_photo_no += next_no
    if current_photo_no >= len(photos):
        current_photo_no = 0
    if current_photo_no < 0:
        current_photo_no = len(photos) - 1
    number_var.set(f'{current_photo_no + 1} of {len(photos)}')
    photo_label.configure(image=photos[current_photo_no])

prev_photo.config(command=lambda : change_photos(-1))
next_photo.config(command=lambda : change_photos(1))


# 运行主循环
root.mainloop()


标签:photo,no,demo,布局,current,result,text,tk
From: https://www.cnblogs.com/code3/p/17642804.html

相关文章

  • Python小项目:利用tkinter开发测手速小游戏
    1简介利用tkinter开发测手速的小游戏,大家10s内可以点击鼠标多少次呢?想测试一下吗?来试试测手速下游戏吧!仅供娱乐!试玩视频:视频链接游戏截图:2代码分模块介绍2.1导入需要的包fromtkinterimport*fromtkinter.messageboximport*2.2定义全局变量def__init__(self,......
  • 使用nltk去掉英文句子中介词以后的句子
    importnltkfromnltk.tokenizeimportword_tokenizefromnltk.tagimportpos_tagnltk.download("punkt")nltk.download("averaged_perceptron_tagger")defremove_prepositional_phrases(sentence,prepositions):words=word_tokenize(s......
  • 现代CSS布局技术:Flexbox和Grid的使用指南和最佳实践
    在现代Web开发中,CSS布局技术变得越来越重要。Flexbox和Grid是两种常用的CSS布局技术,它们可以帮助我们创建灵活、可维护的布局。Flexbox布局Flexbox是一种基于弹性盒子模型的布局技术,它可以让我们更加容易地创建水平或垂直方向上的布局。以下是Flexbox的常见属性:容器属性disp......
  • MT6739(MTK6739)核心板_4G智能模块应用
    MT6739核心板是一款高性能的4G全网通安卓智能模块,提供了丰富的网络制式支持,包括2G/3G/4G移动、联通和电信等。面向全球市场,MTK6739支持TDD-LTE/FDD-LTE/WCDMA/TD-SCDMA/EVDO/CDMA1X/GSM等七种网络制式的智能无线通信模块。MT6739核心板拥有高性价比,是一款符合市场需求......
  • tk一些问题
    TK在Tkinter中,anchor属性用于决定控件在其分配的空间内的对齐方式或锚点。它定义了控件在其容器(例如窗口或框架)中的位置,当控件被分配比其自身大小更大的空间时,该属性可以用来确定控件在该空间内的位置。anchor属性的值是一个字符串,它可以是以下之一:"n":北方(顶部中央)"s":南方(底......
  • 2023-08-18:用go写算法。你会得到一个字符串 text, 你应该把它分成 k 个子字符串 (subte
    2023-08-18:用go写算法。你会得到一个字符串text,你应该把它分成k个子字符串(subtext1,subtext2,…,subtextk)。要求满足:subtexti是非空字符串,所有子字符串的连接等于text,(即subtext1+subtext2+...+subtextk==text),subtexti==subtextk-i+1表示所有i的有......
  • 2023-08-18:用go写算法。你会得到一个字符串 text, 你应该把它分成 k 个子字符串 (subte
    2023-08-18:用go写算法。你会得到一个字符串text,你应该把它分成k个子字符串(subtext1,subtext2,…,subtextk)。要求满足:subtexti是非空字符串,所有子字符串的连接等于text,(即subtext1+subtext2+...+subtextk==text),subtexti==subtextk-i+1表示所有......
  • python监控redis demo
    下载aioredis为了提升性能我们使用一部redispipinstallaioredisdemo:importasyncioimportaioredis#每隔10s获取redis信息asyncdefmonitor_redis(host,port,interval):#建立异步Redis连接redis_uri=f"redis://{host}:{port}"redis=awaitai......
  • 适老化demo
    --------------------------------------------------------html------------------------------------------------------<!doctypehtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>鼠标移入视频播放,鼠标移出播放停止,恢......
  • 实现两栏布局的第二种方式
     目录前言导语 代码部分运行结果前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语实现两栏布......