【python – tkinter optionmenu第一个选项消失了】教程文章相关的互联网学习教程文章

python – 如何在每次在tkinter中显示一个帧时运行一个方法【代码】

我有一个gui应用程序,有几个窗口和按钮前进和后退.为此,我使用控制器并在每次更改窗口时将帧提升到顶部.这是我的控制器代码和典型的框架.import Tkinter as tk # python from tkFileDialog import askopenfilename, asksaveasfilename from analyzer import OptionsTITLE_FONT = ("Helvetica", 18, "bold")class TennisProgram(tk.Tk):def __init__(self, *args, **kwargs):tk.Tk.__init__(self, *args, **kwargs)# the containe...

python_tkinter事件【代码】【图】

1.事件绑定函数(3个)组件.bind(事件类型,事件函数)为一个组件绑定一个操作组件.bind_class(组件类型,事件类型,事件函数)为一个类组件绑定一个操作组件.bind_all(事件类型,事件函数)为所有组件绑定一个操作(所有操作都会当作对主界面的操作) 2.事件类型 3.事件对象 ################事件绑定的案例1:鼠标进入组件变红,离开组件变白 # 单行文本输入框 entry = tkinter.Entry(root) entry.pack() # 事件函数 def ...

python – matplotlib错误 – 没有名为tkinter的模块【代码】

我尝试在Windows 10上通过Pycharm IDE使用matplotlib包.当我运行此代码时:from matplotlib import pyplot我收到以下错误:ImportError: No module named 'tkinter'我知道在python 2.x中它被称为Tkinter,但这不是问题 – 我刚刚安装了一个全新的python 3.5.1. 编辑:此外,我还尝试导入’tkinter’和’Tkinter’ – 这些都没有工作(两者都返回了我提到的错误消息).解决方法: sudo apt-get install python3-tk然后,>> import tkinter...

python – Tkinter.PhotoImage不支持png图像【代码】

我正在使用Tkinter编写GUI并希望在Tkiner.Label中显示png文件.所以我有一些像这样的代码:self.vcode.img = PhotoImage(data=open('test.png').read(), format='png') self.vcode.config(image=self.vcode.img)此代码在我的Linux机器上正确运行.但是当我在我的Windows机器上运行它时,它失败了.我还在其他几台机器上测试过(包括windows和linux),它一直都失败了. Traceback是:Exception in Tkinter callback Traceback (most recent...

鼠标位置Python Tkinter【代码】

有没有办法获得鼠标的位置并将其设置为var?解决方法:你可以设置一个回调来对< Motion>做出反应事件:import Tkinter as tk root = tk.Tk()def motion(event):x, y = event.x, event.yprint('{}, {}'.format(x, y))root.bind('<Motion>', motion) root.mainloop()我不确定你想要什么样的变量.在上面,我将局部变量x和y设置为鼠标坐标. 如果将运动设为类方法,则可以将实例属性self.x和self.y设置为鼠标坐标,然后可以从其他类方法访问...

python – Tkinter窗口中的透明背景【代码】

有没有办法使用Tkinter在Python 3.x中创建“加载屏幕”?我的意思是像Adobe Photoshop的加载屏幕,具有透明度等等.我设法摆脱框架边框已经使用:root.overrideredirect(1)但如果我这样做:root.image = PhotoImage(file=pyloc+'\startup.gif') label = Label(image=root.image) label.pack()图像显示正常,但灰色窗口背景而不是透明度. 有没有办法为窗口添加透明度,但仍能正确显示图像?解决方法:没有跨平台的方法可以让tkinter中的背...

python – tkinter:在画布上使用滚动条【代码】

我正在尝试使画布可滚动.但是,一旦我尝试设置滚动条以使用画布,tkinter似乎完全忽略了我最初为画布设置的尺寸.我已经尝试将它们全部打包在一个框架中,将画布设置为填充框架然后设置框架尺寸,但这会出现同样的问题,除非我将框架设置为填充窗口,这不是我想要的.基本上,我想要一个带有滚动条的固定大小的画布.我当前的代码看起来像这样(在python 3.1中):from tkinter import * root=Tk() frame=Frame(root,width=300,height=300) fra...

如何将JPEG图像插入python Tkinter窗口?【代码】

如何将JPEG图像插入Python 2.7 Tkinter窗口?以下代码有什么问题?该图像称为Aaron.jpg.#!/usr/bin/pythonimport Image import Tkinter window = Tkinter.Tk()window.title("Join") window.geometry("300x300") window.configure(background='grey')imageFile = "Aaron.jpg"window.im1 = Image.open(imageFile)raw_input() window.mainloop()解决方法:试试这个:import tkinter as tk from PIL import ImageTk, Image#This creates...

python将资源管理器拖放到tkinter条目小部件【代码】

我对Python很新.我正在尝试将文件名(包含完整路径)输入到TKinter条目小部件.由于文件名的路径可能很长,我希望能够直接拖放文件从Windows资源管理器.在Perl中我看到了以下内容:use Tk::DropSite; . . my $mw = new MainWindow; $top = $mw->Toplevel; $label_entry = $top->Entry(-width => '45',. -background => 'ivory2')->pack(); $label_entry->DropSite(-dropcommand => \&drop,-droptypes => 'Win32',);我在Python中使用TKi...

python – 如何在Tkinter中显示工具提示?

工具提示是当鼠标在窗口小部件上悬停一段时间时弹出的那些小文本. 如何向tkinter Python应用程序添加工具提示消息? 解决方法:来自Pmw toolkit的Tkinter的Pmw.Balloon级将绘制工具提示. 另外请看一下这个blog post,它使用IDLE中的一些代码来显示Tkinter的工具提示.

使用tkinter在python中播放动画GIF【代码】

我想用python3和tkinter创建一个虚拟宠物风格的游戏.到目前为止,我已经有了主窗口并且已经开始添加标签,但我遇到的问题是播放GIF动画.我在这里搜索并找到了一些答案,但他们不断抛出错误.我发现的结果是使用PhotoImage的gif的索引位置继续在一定范围内.# Loop through the index of the animated gif frame2 = [PhotoImage(file='images/ball-1.gif', format = 'gif -index %i' %i) for i in range(100)]def update(ind):frame = fr...

python – Tkinter IntVar返回PY_VAR0而不是值【代码】

我有一个Checkbutton和一个与之关联的IntVar对象,但是当我尝试获取var的值时,我收到了PY_VAR0. 这是我的代码:from tkinter import *root = Tk()def show_state():print(var)var = IntVar()cbtn = Checkbutton(root, text='Check', variable=var, command=show_state) cbtn.pack()root.mainloop()为什么我得到PY_VAR0?解决方法:var是对Tkinter.IntVar对象的引用.您需要调用其get方法来访问它所代表的值:print(var.get())

python – 这是tkinter中糟糕的编程习惯吗?【代码】

我正在学习使用tkinter编写事件驱动程序,并使用Fredrik Lundh的优秀教程.在那里,他提到最好为框架定义一个类(App)并将程序作为类的实例运行,而不是仅仅启动它:root = Tk() w = Label(root, text = 'hello, world!') w.pack() root.mainloop()我有3个问题: >以这种更简单的方式进行编程是不是很糟糕?>如果我确实定义了一个类,并且回调函数绑定到小部件,那么这些函数都必须在类本身内吗?也就是说,我可以在课堂上有一个按钮,当我点...

python – _tkinter.TclError:没有显示名称和没有$DISPLAY环境变量【代码】

我在服务器中运行一个简单的python脚本:import matplotlib.pyplot as plt import numpy as npx = np.random.randn(60) y = np.random.randn(60)plt.scatter(x, y, s=20)out_png = 'path/to/store/out_file.png' plt.savefig(out_png, dpi=150)我尝试在这个安装了matplotlib 1.5.1的服务器中使用命令python example.py,但失败并出现错误:Traceback (most recent call last):File "example.py", line 7, in <module>plt.scatter(x,...

Python Tkinter 2.7图像问题.【代码】

参见英文答案 > Why does Tkinter image not show up if created in a function? 2个我试图在窗口中显示图像….看起来很简单吧?好吧,我有一个大错误! 我在一个文件中有这个完全相同的代码:import Tkinterroot = Tkinter.Tk() canvas = Tkinter.Canvas(root) canvas.grid(row = 0, column = 0) photo = Tkinter.PhotoImage(file = '/Users/Richy/Desktop/1.gif') image1 = canvas.create_ima...