python gui - 002 tkinter button 调用

用python构建button组件的方法

from tkinter import *  #载入tkinter模块
root = Tk()

def helloButton():  #构建button执行动作
  print('Hello Button!')

Button(root, text = 'Hello Button!', command = helloButton).pack()

root.mainloop()
输出样式
from tkinter import * #导入tkinter库

root = Tk()

def helloButton():  #构建button执行动作
  print('Hello Button!')

#设置button样式
Button(root, text = 'hello button',command = helloButton,relief = FLAT).pack()
Button(root, text = 'hello button',command = helloButton,relief = GROOVE).pack()
Button(root, text = 'hello button',command = helloButton,relief = RAISED).pack()
Button(root, text = 'hello button',command = helloButton,relief = SOLID).pack()
Button(root, text = 'hello button',command = helloButton,relief = SUNKEN).pack()

root.mainloop()

输出如下
输出样式
from tkinter import * #导入tkinter库

root = Tk()

Button(root, text='botton', compound='bottom', bitmap='error').pack()  
Button(root, text='top', compound='top', bitmap='error').pack()  
Button(root, text='right', compound='right', bitmap='error').pack()  
Button(root, text='left', compound='left', bitmap='error').pack()  
Button(root, text='center', compound='center', bitmap='error').pack()  


root.mainloop()
输出样式

使button加载位图

# -*- coding:UTF-8 -*-
from tkinter import *  #载入tkinter模块
root = Tk()
img = PhotoImage(file = 'd:/1.gif')  #如果和主程序不在同一文件夹下,则需要写入详细路径,如果和主程序在同一文件夹下,则只需要写文件名。如果在主程序所在的文件夹内的子文件夹内,则可以写’./****/**.gif‘
button = Button(root,image = img)
button.pack()
root.mainloop()
输出样式
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容