美文网首页
python gui - 002 tkinter button

python gui - 002 tkinter button

作者: wsx9999 | 来源:发表于2018-01-27 11:44 被阅读0次

用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()
输出样式

相关文章

网友评论

      本文标题:python gui - 002 tkinter button

      本文链接:https://www.haomeiwen.com/subject/zvlnaxtx.html