我一直在开发一个简单的程序,可以让按钮输出一些东西。但是当我运行它时,这个
(顺便说一句,我从互联网上得到这个)没有出现。是代码有问题还是什么?请帮助我,以便出现上面的窗口:)
代码:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
Run Code Online (Sandbox Code Playgroud) 我有 ttk.Notebook 类的笔记本小部件。在这个笔记本中,我添加了许多标签:
notebook.add(child=my_object, text='my tab')
如何获取选项卡对象而不是选项卡名称?
所以,有儿童词典。好的,带来下一个问题。
我真正想做的是在 Notebook 中操作活动选项卡的对象。所以我有这个:
notebook.children[notebook.select().split('.')[2]])
Run Code Online (Sandbox Code Playgroud)
但它看起来很丑。不幸的是, select() 返回的小部件名称与 children[] 中的小部件名称格式不完全相同。在这种情况下,是否有任何方法可以将一种格式可靠地转换为另一种格式,或者 split() 永远不会失败?
这是问题的演示:
#!/usr/bin/env python3
from tkinter import *
from tkinter.ttk import *
class App(Tk):
def __init__(self):
super().__init__()
notebook = Notebook(self)
notebook.add(child=Frame(notebook), text='tab1')
notebook.add(child=Frame(notebook), text='tab2')
notebook.add(child=Frame(notebook), text='tab3')
notebook.pack()
print('Tabs are: ', notebook.children.keys())
print('Active tab: ', notebook.select())
print(type(notebook.children[notebook.select().split('.')[2]]))
App().mainloop()
Run Code Online (Sandbox Code Playgroud) 我有一个程序可以根据窗口的大小更改 tkinter 画布上对象的位置。这是可行的,但它会在每个循环中运行,并且当存在大量对象时会显着减慢程序速度,因为它会删除并重新绘制它们。
有没有一种方法可以检查窗口大小的变化,以便在需要时只需运行一次?
我目前正在尝试使用 Tkinter 包使用 Python 来学习制作简单的 GUI。
我为待办事项列表编写了这个程序:
import tkinter
import tkinter.messagebox
import pickle
root = tkinter.Tk()
root.title = ("To Do List")
def add_task():
task = entry_task.get()
if task not in ["", "Do nothing"]:
listbox_tasks.insert(tkinter.END, task)
entry_task.delete(0, tkinter.END)
else:
tkinter.messagebox.showwarning(title="Warning!", message="Doing nothing is not an option!.")
def delete_task():
try:
task_index = listbox_tasks.curselection()[0]
listbox_tasks.delete(task_index)
except:
tkinter.messagebox.showwarning(title="Warning!", message="You need to select a task to delete.")
def load_tasks():
try:
tasks = pickle.load(open("Aufgaben.txt", "rb")) # reading binary
listbox_tasks.delete(0, tkinter.END)
for task in tasks:
listbox_tasks.insert(tkinter.END, task) …Run Code Online (Sandbox Code Playgroud) 我正在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) 我需要一些帮助.我正在使用此方法来帮助我打开一个带有对话框的文件:
from Tkinter import *
import tkFileDialog
fileOpen = Tk()
fileOpen.withdraw() #hiding tkinter window
file_path = tkFileDialog.askopenfilename(
title="Open file", filetypes=[("txt file",".txt"),("All files",".*")])
if file_path != "":
print "you chose file with path:", file_path
else:
print "you didn't open anything!"
print file_path
Run Code Online (Sandbox Code Playgroud)
它工作正常,文件在Python中打开,但我不知道如何在该文件上进一步命令,例如,如果我想读它或写在上面.
我已经尝试将其引用为fileOpen,但这似乎不起作用,我不知道变量会是什么.
如何在我的文本中找到特定文本字符串的位置?
例如,我有文本“Lorem ipsum dolor sat amet, consectetur adipiscing elit.”,当我搜索“ipsum”时,我想找出它的位置。
if "ipsum" in text:
print position
else:
pass
Run Code Online (Sandbox Code Playgroud)
我正在寻找的位置类似于“1.6”如果我想更改文本的颜色,它看起来像这样:
text.tag_add("foo", "1.28", "1.39")
Run Code Online (Sandbox Code Playgroud)
然后为了改变我搜索过的文本的颜色,我需要以某种方式获得它的位置,对吗?我可能是错的,但我正在寻找一些示例或指导。谢谢!:)