Python:打印随机文本(从数据库中获取)并组合 2 个单词,如果我单击一个按钮?我已经有代码
Python: print random text (taken from database) and combine 2 words, if I click a button? I already have code
既然我已经有了要附加的代码,我该如何操作文本并完成这两件事?
- 如果我点击按钮 1:在数据库的垂直列“单词 1”中,是否有一个 random/casual 单词是从多个单词中提取出来的,然后打印在多行文本框中?
- 如果我点击按钮 2:在垂直列“单词 2”中,一个 random/casual 单词是从各种单词中选取的,并且在垂直列“单词 3”中,一个 random/casual 字取自诸字之中。然后这两个词都是 combined/merged 并以这种示例方式(使用 space)打印在多行文本框中:“gggggg oooooooo”或“Hello world”。
我刚开始 Python。你能告诉我2的代码吗
我问的问题好吗?谢谢你,打扰一下
from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3
window=Tk()
window.title("gfff")
window.geometry("700x700")
window.configure(bg='#78c030')
### BUTTON ###
button = Button(window, text="Button", bg='white')
button.pack()
button.place(x=5, y=330)
button2 = Button(window, text="Button 2", bg='white')
button2.pack()
button2.place(x=5, y=380)
### TEXTBOX MULTILINE ###
text = Text(window,width=63,height=38)
text.pack()
text.place(x=180, y=24)
##### FOR BUTTON #####
con = sqlite3.connect('/home/mypc/Scrivania/xxxxxxx/Database.db')
cursor = con.cursor()
cursor.execute('SELECT Word 1 FROM TableExample')
##### FOR BUTTON 2 #####
con = sqlite3.connect('/home/mypc/Scrivania/xxxxxxx/Database.db')
cursor = con.cursor()
cursor.execute('SELECT Word2, Word3 FROM TableExample')
window.mainloop()
我也是 tkinter
的初学者,所以我只是展示简单的例子。 command
在按钮中采用函数名称,以便您可以将函数分配给按钮。我正在创建两个不同的功能,因此只要单击该按钮,该关联功能就会执行。我将 cursor
和第一个文本框 text
用作全局文本框,这就是为什么它们可以在功能中访问并且您可以使用 inesrt
来插入文本。阅读文档以更好地理解
from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3
con = sqlite3.connect('/home/mypc/Scrivania/xxxxxxx/Database.db')
cursor = con.cursor()
window=Tk()
window.title("gfff")
window.geometry("700x700")
window.configure(bg='#78c030')
### TEXTBOX MULTILINE ###
text = Text(window,width=63,height=38)
text.pack()
text.place(x=180, y=24)
def fetch_word_one(): # setting this to button 1
cursor.execute('SELECT Word 1 FROM TableExample ORDER BY RANDOM() LIMIT 1')
word1 = cursor.fetchone()
text.insert(tk.END,word1)
### BUTTON ###
button = Button(window, text="Button", bg='white',command=fetch_word_one)
button.pack()
button.place(x=5, y=330)
def fetch_words_two_three(): # setting this to button 2
cursor.execute('SELECT Word2, Word3 FROM TableExample ORDER BY RANDOM() LIMIT 1')
word2,word3 = cursor.fetchone()
text.insert(tk.END,f"{word2} {word3}")
button2 = Button(window, text="Button 2", bg='white',command=fetch_words_two_three)
button2.pack()
button2.place(x=5, y=380)
window.mainloop()
既然我已经有了要附加的代码,我该如何操作文本并完成这两件事?
- 如果我点击按钮 1:在数据库的垂直列“单词 1”中,是否有一个 random/casual 单词是从多个单词中提取出来的,然后打印在多行文本框中?
- 如果我点击按钮 2:在垂直列“单词 2”中,一个 random/casual 单词是从各种单词中选取的,并且在垂直列“单词 3”中,一个 random/casual 字取自诸字之中。然后这两个词都是 combined/merged 并以这种示例方式(使用 space)打印在多行文本框中:“gggggg oooooooo”或“Hello world”。
我刚开始 Python。你能告诉我2的代码吗 我问的问题好吗?谢谢你,打扰一下
from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3
window=Tk()
window.title("gfff")
window.geometry("700x700")
window.configure(bg='#78c030')
### BUTTON ###
button = Button(window, text="Button", bg='white')
button.pack()
button.place(x=5, y=330)
button2 = Button(window, text="Button 2", bg='white')
button2.pack()
button2.place(x=5, y=380)
### TEXTBOX MULTILINE ###
text = Text(window,width=63,height=38)
text.pack()
text.place(x=180, y=24)
##### FOR BUTTON #####
con = sqlite3.connect('/home/mypc/Scrivania/xxxxxxx/Database.db')
cursor = con.cursor()
cursor.execute('SELECT Word 1 FROM TableExample')
##### FOR BUTTON 2 #####
con = sqlite3.connect('/home/mypc/Scrivania/xxxxxxx/Database.db')
cursor = con.cursor()
cursor.execute('SELECT Word2, Word3 FROM TableExample')
window.mainloop()
我也是 tkinter
的初学者,所以我只是展示简单的例子。 command
在按钮中采用函数名称,以便您可以将函数分配给按钮。我正在创建两个不同的功能,因此只要单击该按钮,该关联功能就会执行。我将 cursor
和第一个文本框 text
用作全局文本框,这就是为什么它们可以在功能中访问并且您可以使用 inesrt
来插入文本。阅读文档以更好地理解
from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3
con = sqlite3.connect('/home/mypc/Scrivania/xxxxxxx/Database.db')
cursor = con.cursor()
window=Tk()
window.title("gfff")
window.geometry("700x700")
window.configure(bg='#78c030')
### TEXTBOX MULTILINE ###
text = Text(window,width=63,height=38)
text.pack()
text.place(x=180, y=24)
def fetch_word_one(): # setting this to button 1
cursor.execute('SELECT Word 1 FROM TableExample ORDER BY RANDOM() LIMIT 1')
word1 = cursor.fetchone()
text.insert(tk.END,word1)
### BUTTON ###
button = Button(window, text="Button", bg='white',command=fetch_word_one)
button.pack()
button.place(x=5, y=330)
def fetch_words_two_three(): # setting this to button 2
cursor.execute('SELECT Word2, Word3 FROM TableExample ORDER BY RANDOM() LIMIT 1')
word2,word3 = cursor.fetchone()
text.insert(tk.END,f"{word2} {word3}")
button2 = Button(window, text="Button 2", bg='white',command=fetch_words_two_three)
button2.pack()
button2.place(x=5, y=380)
window.mainloop()