标签: tkinter

在标签上使用 .config() 时 python 中的错误

好的,所以我有问题。我正在尝试制作一个 GUI 十六进制转换器,但我一直收到同样的错误。我对 Tkinter 没有经验,所以有人可以帮助我吗?这是代码:

from Tkinter import *

def getNum():
   hex1 = e1.get()
   dec1 = int(hex1, 16)
   ol.configure(text=dec1)



root = Tk()

introLabel = Label(root, text='Input Hex Code: ').pack()

e1 = Entry(root)
e1.pack()
e1.focus_set()

inputButton = Button(root, text='Submit Hex Code', command=getNum).pack()

ol = Label(root, text='Hex Code will appear here.').pack()



root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:/Users/The Lodges/Desktop/Python/test.py", line 6, in getNum
ol.configure(text=dec1)
AttributeError: 'NoneType' object has …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-2.7

-1
推荐指数
1
解决办法
2268
查看次数

ImportError:无法导入名称“ Tk”

我正在ubuntu上用python编写程序,但是由于几天的时间,我遇到了这个非常恼人的问题,

Traceback (most recent call last):
  File "/home/tansen/Documents/python2/button25_01JanV1.py", line 1, in <module>
    import Tkinter,ttk
  File "/home/tansen/Documents/python2/Tkinter.py", line 1, in <module>
    from Tkinter import Tk, Label, Button
ImportError: cannot import name 'Tk'
Run Code Online (Sandbox Code Playgroud)

在此程序运行正常之前,我成功运行了多个时间程序

import tkinter.ttk 
from tkinter import *


def viewFile():

    pass

if __name__ == '__main__':

    root = Tk()


    step = LabelFrame(root,text="FILE MANAGER", font = "Arial 20 bold   italic")
    step.grid(row=1, columnspan=7, sticky='W',padx=100, pady=5, ipadx=130, ipady=25)

    Button(step,    text="OpenFile",    font = "Arial 8 bold    italic",    activebackground="turquoise",   width=30, height=5, command=viewFile).grid      (row= 6, column =3) …
Run Code Online (Sandbox Code Playgroud)

python ubuntu tk-toolkit tkinter python-3.x

-1
推荐指数
1
解决办法
4874
查看次数

显示信息警报时,Python tkinter窗口失去焦点

我正在tkinter(python)中构建一个基本的登录系统,当用户输入要注册的详细信息时,我想显示一个tkmessagebox,该框将告诉他们输入中是否有错误。

最初加载GUI时,询问他们是否要登录或注册(工作正常)。假设用户单击注册,则将他们带到第二个屏幕以输入其详细信息(也可以正常工作)。从那里,当他们输入详细信息并单击“注册”时,如果显示错误警报,则GUI的注册部分将失去焦点,并消失在用户选择进入注册屏幕的GUI的第一部分的后面。

显示警报时,有什么方法可以防止我的子窗口失去焦点吗?

def registerCheck(self):

    #print('Username: '+ str(self.UN.get()))
    errorList = ''
    if self.UN.get() == '':
        errorList += 'Username cannot be empty\n'
    if len(self.PW.get()) < 4:
        errorList += 'Password must be more than 4 characters long\n'
    if self.PW.get() != self.PWCF.get():
        errorList += 'Passwords do not match\n'
    if len(self.Email.get()) < 5:
        errorList += 'Email is not valid\n'

    if errorList == '':
        tkMessageBox.showinfo(title="About", message="No Errors Found")
    else:
        tkMessageBox.showerror(title="Errors Found!", message=errorList)


    return
Run Code Online (Sandbox Code Playgroud)

初始画面

注册画面

在此处输入图片说明

python user-interface tkinter python-2.7

-1
推荐指数
1
解决办法
506
查看次数

在for循环中自动创建唯一变量?

我在这里写一下,询问是否有办法让Python在for循环中自动创建新的唯一变量,在下面显示的代码的上下文中:

for row in customerdetails2:
    AA10 = Entry(CustomerEdit)
    AA10.insert(0, row[0])
    AA10.grid(row = editnum, column = 0)
    AA11 = Entry(CustomerEdit)
    AA11.insert(0, row[1])
    AA11.grid(row = editnum, column = 1)
    AA12 = Entry(CustomerEdit)
    AA12.insert(0, row[2])
    AA12.grid(row = editnum, column = 2)
    AA13 = Entry(CustomerEdit)
    AA13.insert(0, row[3])
    AA13.grid(row = editnum, column = 3)
    AA14 = Entry(CustomerEdit)
    AA14.insert(0, row[4])
    AA14.grid(row = editnum, column = 4)
Run Code Online (Sandbox Code Playgroud)

所以基本上对于Python在customerdetails2(它收集信息的数据库)中找到的每一行,我希望它创建一组新的输入字段,但是变量的名称不同,而不是所有的输入字段都被称为'AA10', 'AA11','AA12'等.这可能吗?如果没有,是否有合理的替代方案可以达到相同的效果?抱歉,我仍然是Python的新手,但提前感谢.

python sqlite variables tkinter

-1
推荐指数
1
解决办法
152
查看次数

python中有和没有括号的函数之间的区别

假设代码如下,我有两个关于python的问题:

  1. 为什么不能将带括号或参数的函数传递给add_command
  2. 如果我的CreateWindow函数必须参数,我该怎么办?

这是代码行:

filemenu.add_command(label="update...", command=CreateWindow)
Run Code Online (Sandbox Code Playgroud)

python tkinter

-1
推荐指数
1
解决办法
69
查看次数

当用户双击按钮时,防止函数被调用两次?

我在tkinter/ttk中开发了一个用户界面,它使用标准的ttk.Button小部件来使用'command'参数调用各种函数.问题是,如果用户双击按钮,则会调用该函数两次.这会导致各种各样的问题.双击事件没有绑定任何东西,所以有没有办法禁用第二次单击?我在函数定义的开头尝试了time.sleep()和after(),但是我还没有找到使其工作的方法.我是否需要手动将每个函数绑定到每个按钮的单击并重新分配事件处理程序?任何简单的方法都可以忽略双击?

python tkinter ttk

-1
推荐指数
1
解决办法
1191
查看次数

为什么我的函数会收到一个参数?

我有这个程序,我一直在写,但由于某种原因,该on_closing功能正在接受一个论点

码:

from tkinter import *
from time import sleep

def run():
    global root, target
    target = open("userdata.exe", 'a')
    root = Tk()
    root.attributes("-fullscreen", True)
    root.attributes('-alpha', 0.01)
    root.attributes('-topmost', True)

    def key(event):
        target.write(repr(event.char)+" :")

    frame = Frame(root, width=root.winfo_screenwidth(), height=root.winfo_screenwidth())
    frame.bind("<Key>", key)
    frame.bind("<1>", on_closing)
    frame.pack()
    root.protocol("WM_DELETE_WINDOW", on_closing)
    root.mainloop()

def on_closing():
    root.destroy()
    sleep(10)
    target.close()
    run()


run()
Run Code Online (Sandbox Code Playgroud)

为什么会出现这种情况?

python tkinter key-bindings

-1
推荐指数
1
解决办法
41
查看次数

Python Tkinter 程序因 Entry 崩溃

一直试图用 tkinter 创建一个简单的 gui 计算器,但是一旦我添加了 e = Entry(root) e.pack() 它就会在启动时崩溃我的程序?删除它会使我的程序正常运行吗?很困惑为什么要这样做,并想知道它是否是我拥有多少代码的原因?

import sys
from tkinter import *
import webbrowser
import time

# window Defaults



root = Tk()
root.title("Calculator")
root.geometry("319x212")
root.configure(background="#3b3b3b")
root.iconbitmap("Calculator.ico")

e = Entry(root)
e.pack()




# Addition Numbers

total = 0

def plusOne():
    global total
    total += 1
    print(total)
def plusTwo():
    global total
    total += 2
    print(total)
def plusThree():
    global total
    total += 3
    print(total)
def plusFour():
    global total
    total += 4
    print(total)
def plusFive():
    global total
    total += 5
    print(total) …
Run Code Online (Sandbox Code Playgroud)

python user-interface tkinter calculator tkinter-entry

-1
推荐指数
1
解决办法
249
查看次数

清除文本框时遇到一些麻烦 - tkinter - Python

我已经在互联网上阅读了一些关于清除tkinter上的文本框的线程.基本上每个人都说这很简单:

text.delete("1.0", END)
Run Code Online (Sandbox Code Playgroud)

然而,也许它与我构建它的方式或我调用它的方式有关,但由于某种原因,这对我不起作用.它什么都不做.

我已经尝试重新定位def,并text.delete("1.0", END)以多种方式重写,其中大部分导致我其他错误,但我似乎无法让这个工作.

最终,我想要完成的是,当我点击一个按钮时,文本框将清除,然后填充新信息.

以下是我的代码.

from tkinter import *
from PIL import Image, ImageTk
import functions

class MainWindow(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("pyTicket")

# TOOLBAR ####################################################
        toolbar = Frame(self.parent, bd=1, relief=RAISED)

        self.img = Image.open("Icons\startupcheck.png")
        eimg = ImageTk.PhotoImage(self.img)
        startupButton = Button(toolbar, text="Re-Check ", image=eimg, compound="left", relief=RAISED, command=self.StartUpChecker)
        startupButton.image = eimg
        startupButton.pack(side=RIGHT, padx=2, pady=2)

        toolbar.pack(side=TOP, fill=X)

        self.parent.config(menu=menubar)
        self.pack(anchor=N, side=TOP, fill=X, expand=False)
# TOOLBAR ####################################################

# TEXTBOX ####################################################
        self.textbox …
Run Code Online (Sandbox Code Playgroud)

python text tkinter

-1
推荐指数
1
解决办法
62
查看次数

创建类似对象的简短方法是什么?

from tkinter import *

height = 600
width = 600
root = Tk()
canvas = Canvas(root, width = width, height = height, bg = 'red3')
canvas.pack()

# Code for Fries
canvas.create_polygon(150, 100, 160, 250, 170, 250, 160, 80, fill = 
'yellow',  outline = 'black')
canvas.create_polygon(160, 100, 170, 250, 180, 250, 170, 80, fill = 
'yellow',  outline = 'black')
canvas.create_polygon(170, 100, 180, 250, 190, 250, 180, 80, fill = 
'yellow',  outline = 'black')
canvas.create_polygon(180, 100, 190, 250, 200, 250, 190, 80, …
Run Code Online (Sandbox Code Playgroud)

python tkinter

-1
推荐指数
1
解决办法
89
查看次数