python Tkinter按钮上文字变化怎么弄
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Tkinter按钮上文字变化怎么弄相关的知识,希望对你有一定的参考价值。
我在主窗口设置了一个按钮,按钮里的text是on.我想在我第一次按下它之后变为off,再次按下就由off转变为on,各位大神这是怎么个写法?????反正每按一次就会变化一次。
#!/usr/bin/python2.7#! -*- coding: utf-8 -*-
import Tkinter
root = Tkinter.Tk()
root.title("My tools")
root.geometry('300x300+300+300')
def on_off():
if btonoff['text'] == 'on':
btonoff['text'] = 'off'
else:
btonoff['text'] = 'on'
btonoff = Tkinter.Button(root, text="on", command=on_off)
btonoff.place(x=100, y=160, width=100, height=40)
root.mainloop() 参考技术A python
-
tkinter
button按钮组件是用来添加一个python应用程序中的按钮。这些按钮可以显示文字或图像,表达按钮的目的。当按一下按钮时,您可以附加到一个按钮的函数或方法,该方法自动调用。
按钮组件是用来添加一个python应用程序中的按钮。这些按钮可以显示文字或图像,表达按钮的目的。当按一下按钮时,您可以附加到一个按钮的函数或方法,该方法自动调用.
语法:
这里是一个简单的语法来创建这个widget:
w
=
button
(
master,
option=value,
...
)
参数:master:
这代表了父窗口。 参考技术B import Tkinterroot = Tkinter.Tk()root.title("My tools")root.geometry('300x300+300+300')def on_off(): if btonoff['text'] == 'on': btonoff['text'] = 'off' else: btonoff['text'] = 'on'btonoff = Tkinter.Button(root, text="on", command=on_off)btonoff.place(x=100, y=160, width=100, height=40)root.mainloop()
Tkinter按钮(Button)
Python - Tkinter Button按钮组件是用来添加一个Python应用程序中的按钮。这些按钮可以显示文字或图像,表达按钮的目的。当你按一下按钮时,您可以附加到一个按钮的函数或方法,该方法自动调用。
按钮组件是用来添加一个Python应用程序中的按钮。这些按钮可以显示文字或图像,表达按钮的目的。当你按一下按钮时,您可以附加到一个按钮的函数或方法,该方法自动调用.
语法:
这里是一个简单的语法来创建这个widget:
w = Button ( master, option=value, ... )
参数:
-
master: 这代表了父窗口.
-
options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.
Option | Description |
---|---|
activebackground | Background color when the button is under the cursor. |
activeforeground | Foreground color when the button is under the cursor. |
bd | Border width in pixels. Default is 2. |
bg | Normal background color. |
command | Function or method to be called when the button is clicked. |
fg | Normal foreground (text) color. |
font | Text font to be used for the button‘s label. |
height | Height of the button in text lines (for textual buttons) or pixels (for images). |
highlightcolor | The color of the focus highlight when the widget has focus. |
image | Image to be displayed on the button (instead of text). |
justify | How to show multiple text lines: LEFT to left-justify each line; CENTER to center them; or RIGHT to right-justify. |
padx | Additional padding left and right of the text. |
pady | Additional padding above and below the text. |
relief | Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE. |
state | Set this option to DISABLED to gray out the button and make it unresponsive. Has the value ACTIVE when the mouse is over it. Default is NORMAL. |
underline | Default is -1, meaning that no character of the text on the button will be underlined. If nonnegative, the corresponding text character will be underlined. |
width | Width of the button in letters (if displaying text) or pixels (if displaying an image). |
wraplength | If this value is set to a positive number, the text lines will be wrapped to fit within this length. |
方法:
以下是这个小工具的常用方法:
Medthod | Description |
---|---|
flash() | Causes the button to flash several times between active and normal colors. Leaves the button in the state it was in originally. Ignored if the button is disabled. |
invoke() | Calls the button‘s callback, and returns what that function returns. Has no effect if the button is disabled or there is no callback. |
例子:
自行尝试下面的例子:
import Tkinter import tkMessageBox top = Tkinter.Tk() def helloCallBack(): tkMessageBox.showinfo( "Hello Python", "Hello World") B = Tkinter.Button(top, text ="Hello,Python!少壮不努力,老大学编程.-易百在线教程 - www.yiibai.com", command = helloCallBack) B.pack() top.mainloop()
这将产生以下结果:
以上是关于python Tkinter按钮上文字变化怎么弄的主要内容,如果未能解决你的问题,请参考以下文章
Python3 tkinter基础 Button text,fg 按钮上显示的文字 文字的颜色
python tkinter 怎么改变控件的形状,比如将按钮改成圆形的
如何给 Python Tkinter 给窗口加标题、改变 button 文本?