TKinter—— 2 - 2 Basic Widget - Button 按钮

本文介绍了Tkinter库中的基础组件Button,包括定义、使用方法、实例展示以及如何更改按钮状态。通过b=ttk.Button(root, text, command)创建按钮,并用b.pack()布局。可以使用b.invoke()模拟点击,或通过b.state(['disabled'])使按钮禁用。" 121464522,8335155,Python装饰器详解与应用,"['Python', '函数装饰器', '编程概念', '代码优化', '函数封装']

-- Basic Widget  基础小组件

——————— Button————————————

定义:

按钮往往并不包含文字图片内容

大多时候意味着要用户去点击它 

是一种交互工具

_______________________________________

使用:

b= ttk.Button(root, text, command)

b.pack()

_______________________________________

实例:

import tkinter as tk
    

def write_slogan():
    print("Tkinter is easy to use!")

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()

button = tk.Button(frame, 
                   text="QUIT", 
                   fg="red",
                   command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
                   text="Hello",
                   command=write_slogan)
slogan.pack(side=tk.LEFT)

root.mainloop()

Example Button class

—————————————————————————

更改:

如果想更改设置:

label.config( arg = ) 

—————————————————————————

More:

b.invoke()   # 在command line 模拟 点击 button

b.state(['disabled'])   # button会变灰 无法使用

instate(state)      # check if is the state

import tkinter as tk from tkintertable import TableCanvas, TableModel # Create the main window root = tk.Tk() root.title("Simple Tkintertable Test - 列已固定") root.geometry('600x400') # Create a frame to hold the table tframe = tk.Frame(root) tframe.pack(fill=tk.BOTH, expand=1) # Create a basic table table = TableCanvas(tframe) table.show() # Add some sample data data = { '0': {'A': 1, 'B': 2, 'C': 3}, '1': {'A': 4, 'B': 5, 'C': 6}, '2': {'A': 7, 'B': 8, 'C': 9} } table.model.importDict(data) table.redrawTable() # 彻底禁用列拖拽功能的高级解决方案 def completely_disable_column_drag(table_widget): """彻底禁用列拖拽功能的高级解决方案""" print("应用彻底禁用列拖拽的高级解决方案...") # 方法1: 递归查找并禁用所有内部组件的拖拽功能 def disable_drag_for_all_components(widget): """递归禁用所有子组件的拖拽功能""" try: # 为当前组件添加事件拦截 if hasattr(widget, 'bind'): # 解除所有现有的绑定 try: widget.unbind('<Button-1>') widget.unbind('<B1-Motion>') widget.unbind('<ButtonRelease-1>') except Exception: pass # 绑定我们的事件处理器 widget.bind('<Button-1>', on_intercept_event) widget.bind('<B1-Motion>', on_intercept_event) widget.bind('<ButtonRelease-1>', on_intercept_event) except Exception: pass # 递归处理所有子组件 for child in widget.winfo_children(): disable_drag_for_all_components(child) # 方法2: 事件拦截处理器 def on_intercept_event(event): """事件拦截处理器""" # 检查是否是表头或可能与拖拽相关的区域 widget = event.widget # 检查是否是表头区域 if hasattr(widget, 'tag') and widget.tag == 'heading': # 如果是表头,完全阻止事件传递 return "break" # 检查是否在表头的位置 if hasattr(table_widget, 'header'): header_y = table_widget.header.winfo_y() header_height = table_widget.header.winfo_height() if event.y >= header_y and event.y <= header_y + header_height: return "break" # 对于其他区域,允许默认行为 return # 方法3: 替换主要的拖拽处理方法 if hasattr(table_widget, 'handle_mouse_drag'): # 重写方法以完全阻止拖拽 def disabled_drag(event=None): # 返回"break"阻止所有拖拽事件传递 return "break" table_widget.handle_mouse_drag = disabled_drag print("✓ 已替换handle_mouse_drag方法") # 方法4: 查找并替换其他可能与拖拽相关的方法 def find_and_replace_drag_methods(): # 查找表格对象中可能与拖拽相关的方法 drag_keywords = ['drag', 'move', 'reorder', 'header', 'column'] method_names = [name for name in dir(table_widget) if callable(getattr(table_widget, name))] # 添加一些已知与拖拽相关的方法 known_drag_methods = ['handle_mouse_down', 'handle_mouse_up', 'movecolumn'] for method_name in method_names + known_drag_methods: try: if any(keyword in method_name.lower() for keyword in drag_keywords) and hasattr(table_widget, method_name): # 创建禁用该方法的包装器 def disable_wrapper(event=None): return "break" # 替换原始方法 setattr(table_widget, method_name, disable_wrapper) except Exception: pass # 方法5: 使用Tkinter的底层bindtags功能来拦截所有事件 def override_event_processing(): try: # 获取当前的bindtags current_tags = list(table_widget.bindtags()) # 添加一个自定义的bindtag作为第一个元素 custom_tag = "Custom_NoDrag_Tag" if custom_tag not in current_tags: current_tags.insert(0, custom_tag) # 重新设置bindtags table_widget.bindtags(tuple(current_tags)) # 为自定义标签绑定事件处理器 table_widget.bind_class(custom_tag, '<Button-1>', on_intercept_event, add='+') table_widget.bind_class(custom_tag, '<B1-Motion>', on_intercept_event, add='+') table_widget.bind_class(custom_tag, '<ButtonRelease-1>', on_intercept_event, add='+') except Exception: pass # 方法6: 直接修改表格的内部状态和配置 def modify_internal_state(): # 尝试设置任何可能与拖拽相关的属性 drag_related_attributes = ['dragcols', 'allowdragenabled', 'columnreordering', 'reorderable'] for attr in drag_related_attributes: if hasattr(table_widget, attr): try: setattr(table_widget, attr, False) except Exception: pass # 应用所有方法 disable_drag_for_all_components(table_widget) find_and_replace_drag_methods() override_event_processing() modify_internal_state() print("彻底禁用列拖拽的高级解决方案应用完成 - 表格列现在无法左右调换位置") # 应用彻底禁用列拖拽的解决方案 completely_disable_column_drag(table) # 添加一个说明标签 instruction_label = tk.Label(root, text="表格列已固定,无法通过拖拽改变顺序", font=('Arial', 10), fg='blue') instruction_label.pack(pady=5) # Start the main event loop root.mainloop(),代码中彻底解决表格列可以拖拽左右调换位置的问题?
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值