Tkinter学习笔记(四)

介绍

tix库提供了tk库缺失的常用小控件,如hlist控件,combobox控件,control控件和各种可滚动的小控件,借助tix超级控件可以设计出更加美观的应用程序。
ttk提供了一种与平台无关的窗口工具包

ttk.NoteBook控件

import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.title('主窗口')
root.geometry('400x300+200+200')
frame=ttk.LabelFrame(root,text='ttk.labelframe',width=200,height=100)
frame.pack(expand=1,fill='both')
#创建notebook
tab=ttk.Notebook(frame)
#增加新选项卡
tab1=ttk.Frame(tab)
tab.add(tab1,text='信息窗')#把新的选项卡添加到notebook
tab2=ttk.Frame(tab)
tab.add(tab2,text='综合分析')#把新的选项卡添加到notebook

tab3=ttk.Frame(tab)
tab.add(tab3,text='技术分析')#把新的选项卡添加到notebook

tab4=ttk.Frame(tab)
tab.add(tab4,text='编写代码')#把新的选项卡添加到notebook

tab5=ttk.Frame(tab)
tab.add(tab5,text='模拟回测')#把新的选项卡添加到notebook

tab6=ttk.Frame(tab)
tab.add(tab6,text='双色球')#把新的选项卡添加到notebook

tab7=ttk.Frame(tab)
tab.add(tab7,text='大乐透')#把新的选项卡添加到notebook
tab.pack(expand=1,fill='both')

tab.select(tab4)#选择tab4
root.mainloop()

在这里插入图片描述

ttk.Treeview控件

import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.title('ttk.TreeView演示')
root.geometry('600x500+200+200')
tree=ttk.Treeview(root,height=10)
#""表示根节点,text显示文本,values是隐藏的值
a=tree.insert("",0,"指标",text="指标",values=("1"),open=True)
a1=tree.insert(a,0,"技术指标",text="技术指标",values=("2"))
a2=tree.insert(a,1,"交易系统",text="交易系统",values=("3"))
a3=tree.insert(a,2,"条件选股",text="条件选股",values=("4"))
a4=tree.insert(a,3,"K线",text="K线",values=("5"))

b=tree.insert("",1,"工具",text='工具',values=("6"),open=True)
b1=tree.insert(b,0,"系统设置",text='系统设置',values=("7"))
b2=tree.insert(b,0,"指标排序",text='指标排序',values=("8"))
tree.pack(fill=tk.X)
tree.selection_set("指标")
#show表示指定那些元素树的显示
tree2=ttk.Treeview(root,columns=('col1','col2','col3'),show="headings")
#修改选项制定了
tree2.column('col1',width=100,anchor='center')
tree2.column('col2',width=100,anchor='center')
tree2.column('col3',width=100,anchor='center')
#修改标题选项指定列
tree2.heading('col1',text='股票代码')
tree2.heading('col2',text='日期')
tree2.heading('col3',text='时间')

def click(event):
    item=tree2.selection()[0]
    print('you click on',tree2.item(item,"values"))
for i in range(10):
    tree2.insert("",i,values=('60007','2021年1月8日','11:1'+str(i)))
#双击左键触发事件
tree2.bind("<Double-1>",click)

tree2.pack(fill=tk.X)
root.mainloop()
#9,10

在这里插入图片描述
在这里插入图片描述

form表格几何管理器

import tkinter as tk
from tkinter import ttk
from tkinter import tix
#导入tkinter常量,w
from tkinter.constants import *
root=tix.Tk()

width=300
height=200
x=150
y=150
root.geometry('{}x{}+{}+{}'.format(width,height,x,y))

root.title('tix的form布局')
a=tk.Label(root,text='aaa',relief=tix.SUNKEN,bd=1)
#当right,bottom为none时,控件w为自动宽度
a.form(top=50,left=50,right=None,bottom=None)

b=ttk.Label(root,text='bbb')
b.place(x=50,y=100)

c=tix.Label(root,text='ccc',relief=tix.SUNKEN,bd=1)
c.form(top='%30',left='%50')

#当right=-1时表示w的右边到父控件的右边框
d=tix.Label(root,text='ddd',relief=tix.SUNKEN,bd=1)
d.form(left='%50',top=c,bottom=-1)

root.mainloop()
#11

在这里插入图片描述

tix.Balloon气球窗口控件

tix.Ballon气球控件可以为其他控件提供帮助,当用户将光标移动
到已绑定该控件的窗口中,屏幕就显示一个带有描述性消息的小型弹出式窗口。

import tkinter.tix as tix
from tkinter.constants import *
event=0

def Run(root):
    balloon=DemoBalloon(root)
    balloon.mainloop()
    balloon.destroy()
    
class DemoBalloon:
    def __init__(self,w):
        self.root=w
        self.exit=-1
        #绑定系统级别的事件
        z=w.winfo_toplevel()
        z.wm_protocol("WM_DELETE_WINDOW",lambda self=self: self.quitcmd())
        z.title('tix.balloon演示')
        status=tix.Label(w,width=40,relief=tix.SUNKEN,bd=1)
        status.pack(side=tix.BOTTOM,fill=tix.Y,padx=1,pady=1)
        #建立两个tix按钮
        button1=tix.Button(w,text='关闭窗口',command=self.quitcmd)
        button2=tix.Button(w,text='销毁按钮',command=w.destroy)
        button1.pack(side=tix.TOP,expand=1)
        button2.pack(side=tix.TOP,expand=1)
        #建立tixballon
        b=tix.Balloon(w,statusbar=status)
        b.bind_widget(button1,balloonmsg='关闭',statusmsg='按下这个按钮,关闭窗口')
        b.bind_widget(button2,balloonmsg='删除',statusmsg='按下这个按钮,删除按钮')
    def quitcmd(self):
        self.exit=0
    def mainloop(self):
        a=1
        while self.exit<0 and a>0:
            a=self.root.tk.dooneevent(event)
    def destroy(self):
        self.root.destroy()
if __name__=='__main__':
    root=tix.Tk()
    Run(root)
#12

在这里插入图片描述

tix.ButtonBox按钮框控件

tix.ButtonBox按钮框控件可以添加一下按钮,如“OK”,“cancel”等

tix.combobox组合框控件

用户可以通过链入条目子组件或从列表框子选择组件中选择一个选项。

tix.control控制窗口组件

用户可以通过按上下两个箭头按钮或直接输入数值来调整数值,新值将根据用户定义的上限和下限进行检查。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,让我来回答你的问题。以下是一个使用 Tkinter 和 FileDialog 实现打开和保存文件的示例程序: ```python import tkinter as tk from tkinter import filedialog class App(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def create_widgets(self): self.text = tk.Text(self) self.text.pack() self.open_button = tk.Button(self, text="打开文件", command=self.open_file) self.open_button.pack(side="left") self.save_button = tk.Button(self, text="保存文件", command=self.save_file) self.save_button.pack(side="left") def open_file(self): file_path = filedialog.askopenfilename() if file_path: with open(file_path, "r") as file: file_content = file.read() self.text.delete("1.0", tk.END) self.text.insert(tk.END, file_content) def save_file(self): file_path = filedialog.asksaveasfilename(defaultextension=".txt") if file_path: with open(file_path, "w") as file: file_content = self.text.get("1.0", tk.END) file.write(file_content) root = tk.Tk() app = App(master=root) app.mainloop() ``` 这个程序创建了一个简单的文本编辑器,包含打开文件和保存文件的按钮。当用户点击“打开文件”按钮时,程序会弹出一个文件对话框,用户可以选择要打开的文件。当用户点击“保存文件”按钮时,程序会弹出一个文件对话框,用户可以选择保存文件的路径和文件名。程序会将用户输入的内容保存到文件中。 希望这个示例对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值