【Python:Int()Base 10的文字无效】教程文章相关的互联网学习教程文章

python – asyncio CancelledError和KeyboardInterrupt【代码】

我正在尝试两种方法来阻止无限循环运行: > supervisor_1:以编程方式取消任务> supervisor_2:使用Ctrl C停止任务 虽然supervisor_2在中断时不会抛出任何错误,但我无法让supervisor_1获得任务被销毁但它正在等待!知道为什么吗? 这是代码:import asyncio import aioredis from functools import partialclass Listener:def __init__(self, redis_conn):self.redis_conn = redis_connasync def forever(self, loop_name):counter...

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中将字符串转换为int?【代码】

我为我的小示例应用程序获得的输出如下:Welcome to the Calculator! Please choose what you'd like to do: 0: Addition 1: Subtraction 2: Multiplication 3: Division 4: Quit Application 0 Enter your first number: 1 Enter your second number: 1 Your result is: 11这是因为addition()方法将input()作为字符串而不是数字.我如何将它们用作数字? 这是我的整个脚本:def addition(a, b):return a + bdef subtraction(a, b):...

鼠标位置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 – 不支持的操作数类型:’int’和’str’【代码】

参见英文答案 > How can I concatenate str and int objects? 4个我目前正在学习Python,所以我不知道发生了什么.num1 = int(input("What is your first number? ")) num2 = int(input("What is your second number? ")) num3 = int(input("What is your third number? ")) numlist = [num1, num2, num3] print(numlist) print("Now I will remove the 3rd number") print(numlist.pop(2) + " h...

错误:’int’对象不可订阅 – Python【代码】

我正在尝试一段简单的代码,得到别人的名字和年龄,让他/她知道他们什么时候年满21岁……不考虑否定和所有这些,只是随机的. 我一直得到这个’int’对象不是可下载的错误.name1 = raw_input("What's your name? ") age1 = raw_input ("how old are you? ") x = 0 int([x[age1]]) twentyone = 21 - x print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years."解决方法:问题在于,int([x[age1]])你想要的是什么x = int(age...

python – 线程忽略KeyboardInterrupt异常【代码】

我正在运行这个简单的代码:import threading, timeclass reqthread(threading.Thread): def run(self):for i in range(0, 10):time.sleep(1)print('.')try:thread = reqthread()thread.start() except (KeyboardInterrupt, SystemExit):print('\n! Received keyboard interrupt, quitting threads.\n')但是当我运行它时,它会打印出来$python prova.py . . ^C. . . . . . . . Exception KeyboardInterrupt in <module 'threadin...

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个问题: >以这种更简单的方式进行编程是不是很糟糕?>如果我确实定义了一个类,并且回调函数绑定到小部件,那么这些函数都必须在类本身内吗?也就是说,我可以在课堂上有一个按钮,当我点...