个性化阅读
专注于IT技术分析

Python – Tkinter选择颜色对话框

点击下载

python为GUI(图形用户界面)开发提供了许多选项。Tkinter是除所有其他可用替代方法之外最常用的方法选项。这是使用Tk GUI工具箱开发GUI应用程序的标准方法。开发基本的tkinter应用程序涉及的步骤是:

  1. 导入tkinter模块。
  2. 创建主窗口或容器。
  3. 在主窗口中插入任意数量的小部件。
  4. 将事件触发器应用于所有小部件。

导入tkinter模块的过程与在Python中导入任何其他模块的过程相同。

import tkinter

使用Tkinter创建选择颜色对话框

tkinter模块中有一个名为的包选色器。该tkinter模块软件包有助于开发颜色选择器对话框。该软件包具有一个名为askcolor()扮演主要角色。

askcolor()

该函數属于tkinter模块的colorchooser软件包。该功能有助于创建颜色选择器对话框。调用该函数后, 它将弹出颜色选择器对话框。该函数返回用户选择的颜色的十六进制代码。

语法如下:

colorchooser.askcolor()

例子:

# Python program to create color chooser dialog box
  
# importing tkinter module
from tkinter import *
  
# importing the choosecolor package
from tkinter import choosecolor
  
# Function that will be invoked when the
# button will be clicked in the main window
def choose_color():
  
     # variable to store hexadecimal code of color
     color_code = colorchooser.askcolor(title = "Choose color" ) 
     print (color_code)
  
root = Tk()
button = Button(root, text = "Select color" , command = "choose_color" )
button.pack()
root.geometry( "300x300" )
root.mainloop()

输出如下:

输出1
输出2

注意:对于不同的操作系统, 颜色选择器对话框可能会有所不同。

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » Python – Tkinter选择颜色对话框

评论 抢沙发

评论前必须登录!